MySQL時間函數from_unixtime()date_format()unix_timestamp()now()使用說明
now() 當前時間
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-09-10 19:20:19 |
+---------------------+
1
2
3
4
5
6
unix_timestamp() 當前時間戳
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1536578429 |
+------------------+
1
2
3
4
5
6
unix_timestamp(now()) 當前時間轉換成時間戳
mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
| 1536578445 |
+-----------------------+
1
2
3
4
5
6
unix_timestamp(‘2018-08-08’) 指定時間轉換成時間戳
mysql> select unix_timestamp('2018-08-08');
+------------------------------+
| unix_timestamp('2018-08-08') |
+------------------------------+
| 1533657600 |
+------------------------------+
1
2
3
4
5
6
from_unixtime(1533657600) 將時間戳轉換為時間 默認格式 ‘%Y-%m-%d %H:%i:%s’
mysql> select from_unixtime(1533657600);
+---------------------------+
| from_unixtime(1533657600) |
+---------------------------+
| 2018-08-08 00:00:00 |
+---------------------------+
1
2
3
4
5
6
from_unixtime(1533657600, ‘%Y-%m-%d’) 將時間戳轉換為時間 並進行格式化
mysql> select from_unixtime(1533657600, '%Y-%m-%d');
+---------------------------------------+
| from_unixtime(1533657600, '%Y-%m-%d') |
+---------------------------------------+
| 2018-08-08 |
+---------------------------------------+
1
2
3
4
5
6
查詢時間戳字段 轉換為時間格式顯示
mysql> select from_unixtime(ctime, '%Y-%m-%d') from logs limit 1;
+----------------------------------+
| from_unixtime(ctime, '%Y-%m-%d') |
+----------------------------------+
| 2018-09-18 |
+----------------------------------+
1
2
3
4
5
6
where時間戳字段
mysql> select count(*) from logs where from_unixtime(ctime, '%Y%m%d')=20180808 limit 1;
+----------+
| count(*) |
+----------+
| 12345 |
+----------+
1
2
3
4
5
6
date_format() 將時間進行格式化顯示
mysql> select date_format(now(), '%Y/%m/%d');
+--------------------------------+
| date_format(now(), '%Y/%m/%d') |
+--------------------------------+
| 2018/08/08 |
+--------------------------------+
1
2
3
4
5
6
where時間字段
mysql> select count(*) from logs where date_format(cdate, '%Y%m%d')=20180910 limit 1;
+----------+
| count(*) |
+----------+
| 123 |
+----------+
————————————————
版權聲明:本文為CSDN博主「gocuber」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/gocuber/article/details/80195591