from_unixtime

description

Syntax

DATETIME FROM_UNIXTIME(BIGINT unix_timestamp[, VARCHAR string_format])

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

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

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

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

目前支持的 unix_timestamp 范围为 [0, 32536771199],超出范围的 unix_timestamp 将会得到 NULL

example

1. mysql> select from_unixtime(1196440219);
2. +---------------------------+
3. | from_unixtime(1196440219) |
4. +---------------------------+
5. | 2007-12-01 00:30:19       |
6. +---------------------------+

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

15. mysql> select from_unixtime(1196440219, '%Y-%m-%d');
16. +-----------------------------------------+
17. | from_unixtime(1196440219, '%Y-%m-%d') |
18. +-----------------------------------------+
19. | 2007-12-01                              |
20. +-----------------------------------------+

22. mysql> select from_unixtime(1196440219, '%Y-%m-%d %H:%i:%s');
23. +--------------------------------------------------+
24. | from_unixtime(1196440219, '%Y-%m-%d %H:%i:%s') |
25. +--------------------------------------------------+
26. | 2007-12-01 00:30:19                              |
27. +--------------------------------------------------+

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

1. mysql> select from_second(21474836470);
2. +--------------------------+
3. | from_second(21474836470) |
4. +--------------------------+
5. | 2650-07-06 16:21:10      |
6. +--------------------------+

keywords

1. FROM_UNIXTIME,FROM,UNIXTIME