1)、将时间转换为时间戳
select unix_timestamp('2009-10-26 10-06-07') ;
+---------------------------------------+
| unix_timestamp('2009-10-26 10-06-07') |
+---------------------------------------+
| 1256522767 |
+---------------------------------------+
如果参数为空,则处理为当前时间
2)、将时间戳转换为时间
select from_unixtime(1256540102)
有些应用生成的时间戳是比这个多出三位,是毫秒表示,如果要转换,需要先将最后三位去掉(标准的10位数字,如果是13位的话可以以除以1000的方式),否则返回NULL
mysql> select from_unixtime(1539947679);
+---------------------------+
| from_unixtime(1539947679) |
+---------------------------+
| 2018-10-19 19:14:39 |
+---------------------------+
1 row in set (0.00 sec)
mysql> select from_unixtime(1539947679000);
+------------------------------+
| from_unixtime(1539947679000) |
+------------------------------+
| NULL |
+------------------------------+
1 row in set (0.00 sec)
mysql> select FROM_UNIXTIME(1487655946901/1000);
+-----------------------------------+
| FROM_UNIXTIME(1487655946901/1000) |
+-----------------------------------+
| 2017-02-21 13:45:46.9010 |
+-----------------------------------+
1 row in set (0.00 sec)mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2018-10-22 10:49:28 |
+---------------------+mysql> select UNIX_TIMESTAMP(NOW()) * 1000;
+------------------------------+
| UNIX_TIMESTAMP(NOW()) * 1000 |
+------------------------------+
| 1540176603000 |
+------------------------------+
1 row in set (0.00 sec)
**需要注意的是select UNIX_TIMESTAMP(NOW()) 输出的是秒