【Date and time】:
(补充说明)DATE/TIME 格式:
NOW()、CURRENT_TIMESTAMP()与CURRENT_TIMESTAMP、LOCALTIME()与LOCALTIME、LOCALTIMESTAMP()与LOCALTIMESTAMP:(均为同义词)
返回当前的日期和时间,基于Satement(DATETIME格式)
mysql> SELECT NOW(),CURRENT_TIMESTAMP();
+---------------------+---------------------+
| NOW() | CURRENT_TIMESTAMP() |
+---------------------+---------------------+
| 2015-09-25 14:14:15 | 2015-09-25 14:14:15 |
+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW(),LOCALTIME(),LOCALTIME,LOCALTIMESTAMP(),LOCALTIMESTAMP;
+---------------------+---------------------+---------------------+---------------------+---------------------+
| NOW() | LOCALTIME() | LOCALTIME | LOCALTIMESTAMP() | LOCALTIMESTAMP |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| 2015-09-25 14:21:23 | 2015-09-25 14:21:23 | 2015-09-25 14:21:23 | 2015-09-25 14:21:23 | 2015-09-25 14:21:23 |
+---------------------+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
SYSDATE():返回当前的日期和时间,基于系统时间(DATETIME格式)
mysql> mysql> SELECT NOW(),SYSDATE(),SLEEP(1),NOW(),SYSDATE();
+---------------------+---------------------+----------+---------------------+---------------------+
| NOW() | SYSDATE() | SLEEP(1) | NOW() | SYSDATE() |
+---------------------+---------------------+----------+---------------------+---------------------+
| 2015-09-28 10:49:00 | 2015-09-28 10:49:00 | 0 | 2015-09-28 10:49:00 | 2015-09-28 10:49:01 |
+---------------------+---------------------+----------+---------------------+---------------------+
1 row in set (1.00 sec)
CURDATE()与CURRENT_DATE()与CURRENT_DATE:返回当前的日期,基于服务器主机(DATE格式)
mysql> SELECT CURDATE(),CURRENT_DATE();
+------------+----------------+
| CURDATE() | CURRENT_DATE() |
+------------+----------------+
| 2015-09-25 | 2015-09-25 |
+------------+----------------+
1 row in set (0.00 sec)
CURTIME()与CURRENT_TIME()与CURRENT_TIME: 返回当前的时间,基于服务器主机(TIME格式)
mysql> SELECT CURTIME(),CURRENT_TIME();
+-----------+----------------+
| CURTIME() | CURRENT_TIME() |
+-----------+----------------+
| 14:15:19 | 14:15:19 |
+-----------+----------------+
1 row in set (0.00 sec)
YEAR():返回日期的年份(YEAR格式)
MONTH():返回日期的月份
DAY()与DAYOFMONTH():返回日期的天数
DAYNAME():返回星期几
mysql> SELECT YEAR(NOW()),
-> MONTH(NOW()),
-> DAY(NOW()),
-> DAYOFMONTH(NOW()),
-> DAYNAME(NOW());
+-------------+--------------+------------+-------------------+----------------+
| YEAR(NOW()) | MONTH(NOW()) | DAY(NOW()) | DAYOFMONTH(NOW()) | DAYNAME(NOW()) |
+-------------+--------------+------------+-------------------+----------------+
| 2015 | 9 | 25 | 25 | Friday |
+-------------+--------------+------------+-------------------+----------------+
1 row in set (0.00 sec)
DAYOFWEEK():返回日期对应的星期,1--周日 2--周一 3--周二 4--周三 5--周四 6--周五 7--周六
DAYOFYEAR():返回日期为该年的第多少天
mysql> \! cal
September 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
mysql> SELECT DAYOFWEEK(NOW()),
-> DAYOFYEAR(NOW());
+------------------+------------------+
| DAYOFWEEK(NOW()) | DAYOFYEAR(NOW()) |
+------------------+------------------+
| 2 | 271 |
+------------------+------------------+
1 row in set (0.00 sec)
HOUR():提取时间的小时
MINUTE():提取时间的分钟
SECOND():提取时间的秒数
MICROSECOND():提取时间的微秒
mysql> SELECT HOUR('11:11:12.000123') h,
-> MINUTE('11:11:12.000123') m,
-> SECOND('11:11:12.000123') s,
-> MICROSECOND('11:11:12.000123') ms;
+------+------+------+------+
| h | m | s | ms |
+------+------+------+------+
| 11 | 11 | 12 | 123 |
+------+------+------+------+
1 row in set (0.00 sec)
DATE_FORMAT():将日期时间格式化
mysql> SELECT DATE_FORMAT('2015-10-31 08:30:59','%H:%i:%s');
+-----------------------------------------------+
| DATE_FORMAT('2015-10-31 08:30:59','%H:%i:%s') |
+-----------------------------------------------+
| 08:30:59 |
+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_FORMAT(NOW(),'%y/%m/%d'); -- 也可以这样用
+-------------------------------+
| DATE_FORMAT(NOW(),'%y/%m/%d') |
+-------------------------------+
| 15/09/25 |
+-------------------------------+
1 row in set (0.00 sec)
一般情况下,多数场景可用DATE_FORMAT()代替STR_TO_DATE()。