跳到主要内容
跳到主要内容

from_unixtime

from_unixtime

description

Syntax

DATETIME FROM_UNIXTIME(INT unix_timestamp[, VARCHAR string_format])

将 unix 时间戳转化为对应的 time 格式,返回的格式由 string_format 指定

支持date_format中的format格式,默认为 %Y-%m-%d %H:%i:%s

传入的是整形,返回的是字符串类型

其余 string_format 格式是非法的,返回NULL

如果给定的时间戳小于 0 或大于 253402271999,则返回 NULL。即时间戳范围是:

1970-01-01 00:00:00 ~ 9999-12-31 23:59:59

example

mysql> select from_unixtime(1196440219);
+---------------------------+
| from_unixtime(1196440219) |
+---------------------------+
| 2007-12-01 00:30:19 |
+---------------------------+

mysql> select from_unixtime(1196440219, 'yyyy-MM-dd HH:mm:ss');
+--------------------------------------------------+
| from_unixtime(1196440219, 'yyyy-MM-dd HH:mm:ss') |
+--------------------------------------------------+
| 2007-12-01 00:30:19 |
+--------------------------------------------------+

mysql> select from_unixtime(1196440219, '%Y-%m-%d');
+-----------------------------------------+
| from_unixtime(1196440219, '%Y-%m-%d') |
+-----------------------------------------+
| 2007-12-01 |
+-----------------------------------------+

mysql> select from_unixtime(1196440219, '%Y-%m-%d %H:%i:%s');
+--------------------------------------------------+
| from_unixtime(1196440219, '%Y-%m-%d %H:%i:%s') |
+--------------------------------------------------+
| 2007-12-01 00:30:19 |
+--------------------------------------------------+

对于超过范围的时间戳,可以采用from_second函数 DATETIME FROM_SECOND(BIGINT unix_timestamp)

mysql> select from_second(21474836470);
+--------------------------+
| from_second(21474836470) |
+--------------------------+
| 2650-07-06 16:21:10 |
+--------------------------+

keywords

FROM_UNIXTIME,FROM,UNIXTIME