说实话,在我的应用当中,很少会用到这些MYSQL自带的时间函数,除非是在使用PHPMYADMIN的时候,于是,经常在有人问我,时间上加一天怎么办,减一天怎么办,当前月的最后一天怎么处理,上个月的今天到这个月的今天怎么搞,我都是临时翻手册来加以解释。毕竟大多数的情况下,我们都是会把这些处理交给PHP计算好再使用SQL查询,而真的很少利用自带函数。
在看到Taobao的DBA们把它们整理出来时,不禁暗暗 的YING笑了一番,以后方便了,不用找手册了。。。。。
原文:http://rdc.taobao.com/blog/dba/html/234_mysql_date_func.html
作者:丁原
内容:
通常我们会有一些时间的转换需求,比如要统计某个时间段的收入,比如要截取某个时间的年份,比如要根据某个日期推算出是星期几等个,这些都可以通过Mysql自带的时间函数很容易实现。因为我对Mysql的函数还不熟,而通常又会调用这些时间函数,这边稍加总结以便查询。
–返回当前时间
+---------------------+-------------+---------------------+
| now() | date(now()) | sysdate() |
+---------------------+-------------+---------------------+
| 2008-12-02 10:11:36 | 2008-12-02 | 2008-12-02 10:11:36 |
+---------------------+-------------+---------------------+
1 row in set (0.00 sec)
mysql> select curdate(),curdate()+0,curtime(),curtime()+0;
+---------------------+-------------+---------------------+
| curdate() | curdate()+0 | curtime() | curtime()+0 |
+---------------------+-------------+---------------------+
| 2008-12-02 | 20081202 | 10:00:33 | 100033.000000 |
+---------------------+-------------+---------------------+
–返回日期当月最后一天
+------------------------+
| last_day('2008-12-02') |
+------------------------+
| 2008-12-31 |
+------------------------+
1 row in set (0.00 sec)
–返回日期的星期几
+-----------------------+-------------------------+
| dayname('2008-12-02') | dayofweek('2008-12-02') |
+-----------------------+-------------------------+
| tuesday | 3 |
+-----------------------+-------------------------+
1 row in set (0.00 sec)
–返回日期的年,月,日
+---------------------+--------------------+-------------------+
| month('2008-12-02') | year('2008-12-02') | day('2008-12-02') |
+---------------------+--------------------+-------------------+
| 12 | 2008 | 2 |
+---------------------+--------------------+-------------------+
1 row in set (0.00 sec)
–返回日期的小时,分,秒
+------------------+--------------------+--------------------+
| hour('10:05:03') | minute('10:05:03') | second('10:05:03') |
+------------------+--------------------+--------------------+
| 10 | 5 | 3 |
+------------------+--------------------+--------------------+
1 row in set (0.00 sec)
1.subdate(d,t):起始时间加上一段时间(year,month,day…)
+-----------------------------------------+---------------------------+
| date_add('1998-01-02', interval 31 day) | adddate('1998-01-02', 31) |
+-----------------------------------------+---------------------------+
| 1998-02-02 | 1998-02-02 |
+-----------------------------------------+---------------------------+
1 row in set (0.00 sec)
mysql> select date_add('1998-01-02',interval 2 year);
+-----------------------------------------------------+
| date_add('1998-01-02', interval 2 year)
+-----------------------------------------------------+
| 2000-01-02
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('1998-01-02', interval 2 hour);
+-----------------------------------------------------+
| date_add('1998-01-02', interval 2 hour)
+-----------------------------------------------------+
| 1998-01-02 02:00:00
+-----------------------------------------------------+
1 row in set (0.00 sec)
2.subdate(d,t):起始时间减去一段时间
+----------------------------------------+---------------------------+
| subdate('1998-01-02', interval 31 day) | subdate('1998-01-02', 31) |
+----------------------------------------+---------------------------+
| 1997-12-02 | 1997-12-02 |
+----------------------------------------+---------------------------+
1 row in set (0.00 sec)
3.addtime(d,t):起始时间d加入时间t
+-------------------------------------------+--------------------------------+
| addtime('1997-12-31 23:59:50','00:00:05') | addtime('23:59:50','00:00:05') |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:55 | 23:59:55 |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)
4.subtime(d,t):起始时间d减去时间t
+-------------------------------------------+--------------------------------+
| subtime('1997-12-31 23:59:50','00:00:05') | subtime('23:59:50','00:00:05') |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:45 | 23:59:45 |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)
5.datediff(d1,d2):返回起始时间d1和结束时间d2之间的天数
+----------------------------------------------+
| datediff('1997-12-31 23:59:59','1997-12-30') |
+----------------------------------------------+
| 1 |
+----------------------------------------------+
1 row in set (0.00 sec)
6.date_format(date,format):根据format字符串显示date值的格式
+---------------------------------------------------------+
| date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s') |
+---------------------------------------------------------+
| 2008 12 12 22:23:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
7.str_to_date(str,format) 字符串转化为时间
+-----------------------------------------------+
| str_to_date('04/31/2004', '%m/%d/%y %h:%i:s') |
+-----------------------------------------------+
| 2004-04-31 00:00:00 |
+-----------------------------------------------+
1 row in set (0.00 sec)
8.timestamp(expr) , timestamp(expr,expr2) :
对于一个单参数,该函数将日期或日期时间表达式 expr 作为日期时间值返回.对于两个参数, 它将时间表达式 expr2添加到日期或日期时间表达式 expr 中,将theresult作为日期时间值返回
+-------------------------+---------------------------------------------+
| timestamp('2003-12-31') | timestamp('2003-12-31 12:00:00','12:00:00') |
+-------------------------+---------------------------------------------+
| 2003-12-31 00:00:00 | 2004-01-01 00:00:00 |
+-------------------------+---------------------------------------------+
1 row in set (0.00 sec)
9.取当天0点0分,下一天0点0分
+----------------------------+---------------------------------------+
| timestamp(date(sysdate())) | timestamp(adddate(date(sysdate()),1)) |
+----------------------------+---------------------------------------+
| 2008-12-02 00:00:00 | 2008-12-03 00:00:00 |
+----------------------------+---------------------------------------+
1 row in set (0.00 sec)
--EOF--