背景
数据库中经常出现需要UNIX时间戳与日期时间转换的场景,这里简单对其中的转换进行记录备忘
UNIX时间戳转日期时间:FROM_UNIXTIME()
mysql> select FROM_UNIXTIME(1494254907);
+---------------------------+
| FROM_UNIXTIME(1494254907) |
+---------------------------+
| 2017-05-08 22:48:27 |
+---------------------------+
1 row in set (0.00 sec)
日期时间转UNIX时间戳:UNIX_TIMESTAMP()
mysql> select UNIX_TIMESTAMP('2017-05-08 22:48:27');
+---------------------------------------+
| UNIX_TIMESTAMP('2017-05-08 22:48:27') |
+---------------------------------------+
| 1494254907 |
+---------------------------------------+
1 row in set (0.00 sec)