Home >Backend Development >PHP Tutorial >Timestamp and date conversion
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If called without parameters, a Unix timestamp (the number of seconds after '1970-01-01 00:00:00' GMT) is returned as an unsigned integer. If UNIX_TIMESTAMP() is called with date, it will return the parameter value as the number of seconds after GMT '1970-01-01 00:00:00'. date can be a DATE string, a DATETIME string, a TIMESTAMP, or a local time number in YYMMDD or YYYMMDD format.
mysql> SELECT UNIX_TIMESTAMP(); -> 882226357 mysql> SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00'); -> 875996580
When UNIX_TIMESTAMP is used on a TIMESTAMP column, the function returns the internal timestamp value directly without any implicit "string-to-Unix-timestamp" conversion. If you pass UNIX_TIMESTAMP() an overflow date, it will return 0, but please note that only basic range checks will be performed (years from 1970 to 2037, months from 01 to 12, days from 01 to 31).
Example of converting timestamp and time under PHP:
PHP code
$timestamp = 1210003200; $datetime = date('Y-m-d H:i:s', $timestamp); echo "该时间戳代表的时间:", $datetime, "<br>\n"; echo "从此时间重新转回时间戳:", strtotime($datetime), "<br>\n";
Example of converting timestamp under MySQL:
SQL code
select from_unixtime(1210003200) datetime, unix_timestamp(from_unixtime(1210003200)) timestamp;