How to store UNIX timestamps with MySQL
Actually, you have to use either bigint
or varchar
because the maximum for int(11)
is 2'147'483'647 (more info here).
Then, as the previous answers say, you have to manually insert UNIX_TIMESTAMP()
You can continue using an unsigned INT, but you'll have to manually set the timestamp on insert (UNIX_TIMESTAMP()
).
Or you can use the TIMESTAMP type with the default CURRENT_TIMESTAMP (which is stored as an int behind the scenes) and convert it to an int when selecting:SELECT UNIX_TIMESTAMP(foo_field) FROM foo_table
Reference - Is it possible to create a column with a UNIX_TIMESTAMP default in MySQL?
A Unix timestamp is a large integer (the number of seconds since 1970-01-01 00:00:00 UTC), so INT(11)
is the correct datatype.
Unfortunately, I don't think there's any way to specify a default that will insert the current timestamp. You'll need to call UNIX_TIMESTAMP()
explicitly when inserting, and use that. Function calls aren't allowed in DEFAULT
specifications.