UNIX_TIMESTAMP
描述
将 Date 或者 Datetime 类型转化为 unix 时间戳。
如果没有参数,则是将当前的时间转化为时间戳。
参数需要是 Date 或者 Datetime 类型。
Format 的格式请参阅 date_format
函数的格式说明。
该函数受时区影响。
语法
UNIX_TIMESTAMP([DATETIME date[, STRING fmt]])
参数
参数 | 描述 |
---|---|
<date> | 待转换的日期时间值,类型为 datetime 或 date 类型,可转换范围为 '1970-01-01 00:00:01.000000 UTC' 至 '3001-01-19 03:14:07.999999 UTC'。 |
<fmt> | date 参数指代需要转换为时间戳的特定部分,其类型为 string 。若提供该参数,则仅将与格式匹配的部分转换为时间戳。 |
返回值
根据输入返回两种类型
1.若是输入的 date
(只有 datetime 类型才可以有 scale 不为零) scale 不为 0 或者带有 format
参数
返回一个时间戳,类型为 Decimal,最高六位小数精度
2.若是输入的 date
scale 为 0 并且不带有 format
参数
返回一个时间戳,类型为 INT
对于在 1970-01-01 00:00:01.000000 UTC 之前或 3001-01-19 03:14:07.999999 UTC 之后的时间,该函数将返回 0。
任意参数为 null 则返回 null
举例
---以下都是在 UTC 时区返回的结果
set time_zone= 'UTC';
---显示当前时间的时间戳
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1753933330 |
+------------------+
---输入一个 datetime 显示该时间的
mysql> select unix_timestamp('2007-11-30 10:30:19');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30:19') |
+---------------------------------------+
| 1196389819 |
+---------------------------------------+
---匹配 format 显示给出的 datetime 对应时间戳
mysql> select unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s');
+------------------------------------------------------------+
| unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s') |
+------------------------------------------------------------+
| 1196389819.000000 |
+------------------------------------------------------------+
---只匹配年日月显示时间戳
mysql> select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d');
+-----------------------------------------------------+
| unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d') |
+-----------------------------------------------------+
| 1196352000.000000 |
+-----------------------------------------------------+
---带有其他字符匹配
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', '%Y-%m-%d %H:%i%%3A%s') |
+-----------------------------------------------------------------+
| 1196389819.000000 |
+-----------------------------------------------------------------+
---超出最小范围内的时间返回 0
mysql> SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00');
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 00:00:00') |
+---------------------------------------+
| 0 |
+---------------------------------------+
---输入时间并且 scale 不为 0
mysql> SELECT UNIX_TIMESTAMP('2015-11-13 10:20:19.123');
+-------------------------------------------+
| UNIX_TIMESTAMP('2015-11-13 10:20:19.123') |
+-------------------------------------------+
| 1447381219.123 |
+-------------------------------------------+
---超过允许时间的最大范围
mysql> SELECT UNIX_TIMESTAMP('3001-01-19 03:14:07.999999');
+----------------------------------------------+
| UNIX_TIMESTAMP('3001-01-19 03:14:07.999999') |
+----------------------------------------------+
| 0.000000 |
+----------------------------------------------+
---任意参数为 null 则返回 Null
mysql> select unix_timestamp(NULL);
+----------------------+
| unix_timestamp(NULL) |
+----------------------+
| NULL |
+----------------------+
mysql> select unix_timestamp('2038-01-19 11:14:08',null);
+--------------------------------------------+
| unix_timestamp('2038-01-19 11:14:08',null) |
+--------------------------------------------+
| NULL |
+--------------------------------------------+
keywords
UNIX_TIMESTAMP,UNIX,TIMESTAMP