跳到主要内容
版本: 最新版本-3.5

unix_timestamp

将 DATE 或 DATETIME 值转换为 UNIX 时间戳。

如果未指定参数,此函数会将当前时间转换为 UNIX 时间戳。

The date parameter must be of the DATE or DATETIME type.(date 参数必须是 DATE 或 DATETIME 类型。)

对于 1970-01-01 00:00:00 之前或 2038-01-19 11:14:07 之后的时间,此函数返回 0。

有关日期格式的更多信息,请参阅date_format

此函数可能会为不同的时区返回不同的结果。 有关详细信息,请参见配置时区

语法

BIGINT UNIX_TIMESTAMP()
BIGINT UNIX_TIMESTAMP(DATETIME date)
BIGINT UNIX_TIMESTAMP(DATETIME date, STRING fmt)

示例

MySQL > select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1558589570 |
+------------------+

MySQL > select unix_timestamp('2007-11-30 10:30:19');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30:19') |
+---------------------------------------+
| 1196389819 |
+---------------------------------------+

MySQL > select unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30-19') |
+---------------------------------------+
| 1196389819 |
+---------------------------------------+

MySQL > select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d %H:%i%%3A%s');
+---------------------------------------+
|unix_timestamp('2007-11-30 10:30%3A19')|
+---------------------------------------+
| 1196389819 |
+---------------------------------------+

MySQL > select unix_timestamp('1969-01-01 00:00:00');
+---------------------------------------+
| unix_timestamp('1969-01-01 00:00:00') |
+---------------------------------------+
| 0 |
+---------------------------------------+

关键词

UNIX_TIMESTAMP,UNIX,TIMESTAMP