UNIX_TIMESTAMP(date) 函数把普通格式的日期时间值转换成 UNIX 时间戳,与 FROM_UNIXTIME() 函数互为反函数。
(1)使用 UNIX_TIMESTAMP(date) 函数将普通格式时间转换成 UNIX 时间戳转。如下:
mysql> select UNIX_TIMESTAMP('2022-04-26 12:00:30'); +---------------------------------------+ | UNIX_TIMESTAMP('2022-04-26 12:00:30') | +---------------------------------------+ | 1650945630 | +---------------------------------------+ 1 row in set (0.03 sec)
(2)使用 UNIX_TIMESTAMP() 函数输出当前日期时间戳,如下:
mysql> select UNIX_TIMESTAMP(), UNIX_TIMESTAMP(NOW()); +------------------+-----------------------+ | UNIX_TIMESTAMP() | UNIX_TIMESTAMP(NOW()) | +------------------+-----------------------+ | 1650947884 | 1650947884 | +------------------+-----------------------+ 1 row in set (0.02 sec)