简述
时间的时间戳表示:默认格式为'%Y-%M-%d %H:%m:%s',例如:2019-07-06 15:18:47。时间的长整型表示形式:长度为10位,即表示的是秒数,从1970年1月1日开始的。
获取当前时间的时间戳形式
mysql
> select current_timestamp
;
+---------------------+
| current_timestamp
|
+---------------------+
| 2019-07-06 15:18:47
|
+---------------------+
1 row
in set (0.01 sec
)
获取当前时间的长整形形式
mysql
> select UNIX_TIMESTAMP
();
+------------------+
| UNIX_TIMESTAMP
() |
+------------------+
| 1562397569
|
+------------------+
1 row
in set (0.01 sec
)
时间戳timestamp转换为长整形long: unix_timestamp({timestamp})
mysql
> select unix_timestamp
('2019-07-06 15:18:47') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562397527
|
+--------------+
1 row
in set (0.01 sec
)
mysql
> select unix_timestamp
('2019-07-06 15:18') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562397480
|
+--------------+
1 row
in set (0.00 sec
)
mysql
> select unix_timestamp
('2019-07-06 15') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562396400
|
+--------------+
1 row
in set (0.01 sec
)
mysql
> select unix_timestamp
('2019-07-06') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562342400
|
+--------------+
1 row
in set (0.00 sec
)
mysql
> select unix_timestamp
('2019-07') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 0.000000
|
+--------------+
1 row
in set, 1 warning
(0.00 sec
)
mysql
> select unix_timestamp
('20190706') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562342400
|
+--------------+
1 row
in set (0.02 sec
)
mysql
> select unix_timestamp
('2019-07-06') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562342400
|
+--------------+
1 row
in set (0.00 sec
)
mysql
> select unix_timestamp
('2019/07/06') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 1562342400
|
+--------------+
1 row
in set (0.00 sec
)
通过unix_timestamp可以进行时间戳转换的格式有:
年月日时分秒:%Y-%M-%d %H:%m:%s、%Y/%M/%d %H:%m:%s、%Y/%M/%d %H/%m/%s年月日时分:%Y-%M-%d %H:%m、%Y/%M/%d %H:%m、%Y/%M/%d %H/%m年月日时:%Y-%M-%d %H、%Y/%M/%d %H年月日:%Y-%M-%d、%Y/%M/%d、%Y%M%d
长整形long转换为时间戳timestamp: from_unixtime({long}[,{format}])
mysql
> select from_unixtime
(1562397527,
'%Y-%M-%d %H:%m:%s') AS 当前时间
;
+---------------------+
| 当前时间
|
+---------------------+
| 2019-07-06 15:07:47
|
+---------------------+
1 row
in set (0.01 sec
)
mysql
> select from_unixtime
(1562397527,
'%Y-%M-%d') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| 2019-07-06
|
+--------------+
1 row
in set (0.00 sec
)
mysql
> select from_unixtime
(1562397527,
'%Y年%M月%d日') AS 当前时间
;
+-------------------+
| 当前时间
|
+-------------------+
| 2019年07月06日
|
+-------------------+
1 row
in set (0.01 sec
)
mysql
> select from_unixtime
(1562397527000,
'%Y年%M月%d日') AS 当前时间
;
+--------------+
| 当前时间
|
+--------------+
| NULL
|
+--------------+
1 row
in set (0.01 sec
)
通过from_unixtime可以转换的长整形需为长度为10的秒数,13位的毫秒数不支持。
通过from_unixtime可以转换成的格式则决定于传入的格式化参数,主要参数有:
%Y: 年%M: 月%d: 日%H: 时%m: 分%s: 秒