Saturday, July 16, 2016

Storing and Retrieving epoch/Unix time in MySQL DB

Epoch time - The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'.

Select Unix time from DATE column
---------------------------------------------
mysql> SELECT unix_timestamp('2012-12-28 19:09:03');
+---------------------------------------+
| unix_timestamp('2012-12-28 19:09:03') |
+---------------------------------------+
|                            1356721743 |
+---------------------------------------+
1 row in set (0.10 sec)

Select date from Unix time
----------------------------------
mysql> SELECT from_unixtime(1356721743);
+---------------------------+
| from_unixtime(1356721743) |
+---------------------------+
| 2012-12-28 19:09:03       |
+---------------------------+
1 row in set (0.11 sec)

References:
http://www.epochconverter.com/

No comments:

Post a Comment