I want to store PHP\'s microtime as my timestamp in MySQL.
I\'ve been told it\'s best to store it in DECIMAL
, but I can\'t find an ideal size.
Does
In MySQL 5.6.4 and above, the native DATETIME and TIMESTAMP types can support fractional seconds. Thus, you can store a timestamp with microsecond resolution in a DATETIME(6)
or a TIMESTAMP(6)
column.
To convert PHP microtime() return values to MySQL datetime format, you can use the MySQL FROM_UNIXTIME() function or, if you're using the PHP DateTime class, DateTime::format(). Note that the PHP date() function does not currently support microsecond timestamps. (It does have a format code for microseconds, u
, but it always maps it to 000000
.)
For older MySQL versions, which cannot store microseconds in their native datetime types, you should use either DOUBLE
, BIGINT
(with the values expressed in microseconds, i.e. multiplied by 1,000,000) or DECIMAL(16,6)
(which should be enough for a few hundred years yet).