在 MySql 中经常会用到日期,关于常用的日期函数,做了以下的总结:
1 . now()
作用; 获取当前的日期
除此之外,获取当前日期的函数还有: current_timestamp(); current_time; localtime(); localtime; localtimestamp(); localtimestamp;但是这些日期函数,与 now() 的效果相同, 为了方便记忆,建议使用 now() 来代替上面的函数。
mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select datediff(now(),'2015-1-1');
+----------------------------+
| datediff(now(),'2015-1-1') |
+----------------------------+
| |
+----------------------------+
row in set mysql> select localtime();
+---------------------+
| localtime() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtime;
+---------------------+
| localtime |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtimestamp;
+---------------------+
| localtimestamp |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select localtimestamp();
+---------------------+
| localtimestamp() |
+---------------------+
| -- :: |
+---------------------+
row in set mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| -- :: |
+---------------------+
row in set
2. sysdate()
作用: 这个日期函数与 now() 类似,不同的地方是: now() 在执行开始时就得到的确定,而 sysdate() 则是在运行时动态得到 值。
如下所示:
mysql> select now(), sleep(30), now();
+---------------------+-----------+---------------------+
| now() | sleep(30) | now() |
+---------------------+-----------+---------------------+
| 2015-05-13 15:01:49 | 0 | 2015-05-13 15:01:49 |
+---------------------+-----------+---------------------+
1 row in set mysql> select sysdate(), sleep(30), sysdate();
+---------------------+-----------+---------------------+
| sysdate() | sleep(30) | sysdate() |
+---------------------+-----------+---------------------+
| 2015-05-13 15:02:52 | 0 | 2015-05-13 15:03:22 |
+---------------------+-----------+---------------------+
1 row in set
在用 now() 时,在途中 sleep 了 30秒 ,但是 Now() 的结果是相同的, 而在 sysdate() 中 sleep 了 30秒, 结果就不同了,结果是 sleep 30 秒后的值
3. curdate()
作用; 获取当前日期
函数 current_date(); current_date; 与 curdate() 功能一样
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2015-05-13 |
+------------+
1 row in set mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2015-05-13 |
+----------------+
1 row in set mysql> select current_date;
+--------------+
| current_date |
+--------------+
| 2015-05-13 |
+--------------+
1 row in set
4. curtime()
作用: 获取当前时间 ,其功能与 current_time current_time() 是一样的
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 15:04:42 |
+-----------+
1 row in set mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 15:05:05 |
+----------------+
1 row in set mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 15:05:09 |
+--------------+
1 row in set
5. utc_date(); utc_time(); utc_timestamp();
作用: 获取当前 utc 时间的函数
mysql> select utc_timestamp(), utc_date(),utc_time(),now();
+---------------------+------------+------------+---------------------+
| utc_timestamp() | utc_date() | utc_time() | now() |
+---------------------+------------+------------+---------------------+
| 2015-05-13 07:05:51 | 2015-05-13 | 07:05:51 | 2015-05-13 15:05:51 |
+---------------------+------------+------------+---------------------+
1 row in set
6. date(); year(); month(); day(); time(); week(); hour(); minute(); second(); microsecond();
作用; 获取日期中的部分值
mysql> set @date='2015-05-05 14:23:34.345687'
-> select date(@date)
-> select time(@date)
-> select year(@date)
-> select quarter(@date)
-> select month(@date)
-> select week(@date)
-> select day(@date)
-> select hour(@date)
-> select minute(@date)
-> select second(@date)
-> select microsecond(@date);
7. extract()
作用: 和上面所列举的函数功能一样, 只是函数的书写方式不同
mysql> set @date='2015-05-05 14:23:34.345687'
-> select extract(year from @date)
8. last_day()
作用: 返回月份的最后一天
9. datedifff()
作用: 计算两个日期间的相差的天数
mysql> select datediff(now(),'2015-05-05');
+------------------------------+
| datediff(now(),'2015-05-05') |
+------------------------------+
| 8 |
+------------------------------+
10. str_to_date(str,format)
作用: 可以把一些杂乱无章的字符串转换成日期格式,也可以转换成时间
有关更多的 Mysql 函数,可以参考 Mysql 在线手册:http://www.cbi.pku.edu.cn/chinese/documents/csdoc/mysql/manual_toc.html