DATA_ADD(date,INTERVAL expr unit)、DATE_SUB(date,INTERVAL expr unit)
这些函数进行时间运算;date参数指定开始日期的日期或日期时间值,expr是一个表达式指定要添加或减去的值的间隔的开始日期
unit Value | Expected expr Format |
MICROSECOND | MICROSECONDS |
SECOND | SECONDS |
MINUTE | MINUTES |
HOUR | HOURS |
DAY | DAYS |
WEEK | WEEKS |
MONTH | MONTHS |
QUARTER | QUARTERS |
YEAR | YEARS |
SECOND_MICROSECOND | 'SECONDS.MICROSECONDS' |
MINUTE_MICROSECOND | 'MINUTES:SECONDS.MICROSECONDS' |
MINUTE_SECOND | 'MINUTES:SECONDS' |
HOUR_MICROSECOND | 'HOURS:MINUTES:SECONDS.MICROSECONDS' |
HOUR_SECOND | 'HOURS:MINUTES:SECONDS' |
HOUR_MINUTE | 'HOURS:MINUTES' |
DAY_MICROSECOND | 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS' |
DAY_SECOND | 'DAYS HOURS:MINUTES:SECONDS' |
DAY_MINUTE | 'DAYS HOURS:MINUTES' |
DAY_HOUR | 'DAYS HOURS' |
YEAR_MONTH | 'YEARS-MONTHS' |
也可以写作
date + INTERVAL expr unitdate - INTERVAL expr unit
mysql> select '2008-1-31' + interval 1 month;+--------------------------------+| '2008-1-31' + interval 1 month |+--------------------------------+| 2008-02-29 |+--------------------------------+1 row in set (0.00 sec)mysql> select date_add('2008-1-31',interval 1 month);+----------------------------------------+| date_add('2008-1-31',interval 1 month) |+----------------------------------------+| 2008-02-29 |+----------------------------------------+1 row in set (0.00 sec)
获取一天的开始和最后的时间
mysql> select '2015-4-4 0:0:0' + interval 1 day - interval 1 second;+-------------------------------------------------------+| '2015-4-4 0:0:0' + interval 1 day - interval 1 second |+-------------------------------------------------------+| 2015-04-04 23:59:59 |+-------------------------------------------------------+1 row in set (0.00 sec)
mysql> SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002'); -> '2008-01-02 01:01:01.000001'mysql> SELECT ADDTIME('01:00:00.999999', '02:00:00.999998'); -> '03:00:01.999997'
mysql> select curdate();+------------+| curdate() |+------------+| 2015-04-03 |+------------+1 row in set (0.00 sec)mysql> select curdate() + 0;+---------------+| curdate() + 0 |+---------------+| 20150403 |+---------------+1 row in set (0.00 sec)
mysql> select curtime();+-----------+| curtime() |+-----------+| 14:06:30 |+-----------+1 row in set (0.00 sec)mysql> select curtime() + 0;+---------------+| curtime() + 0 |+---------------+| 140641.000000 |+---------------+1 row in set (0.00 sec)
格式字符串中可以使用 '%' 说明符
常用的:
%a -- 星期几的缩写 (Sun..Sat)
%b -- 月份的缩写 (Jan...Dec)
%c -- 月份 (0--12)
%D -- 天数 加上了英文后缀 (0th,1sh...)
%d,%e -- 月份中的天数 (0-31)
%H -- 24小时制 (00- 23)
%h,%I -- 12小时制 (00-12)
%i -- 分钟 (00-59)
%M -- 月份 (january,december)
%m -- 月份 (00-12)
%S,%s -- 秒 (00-59)
%Y -- 年 4位
%y -- 年 2位
mysql> select date_format(now(),'%Y/%m/%d %H:%m:%s');+----------------------------------------+| date_format(now(),'%Y/%m/%d %H:%m:%s') |+----------------------------------------+| 2015/04/03 14:04:05 |+----------------------------------------+1 row in set (0.00 sec)
mysql> select date(now());+-----------------+| date(curdate()) |+-----------------+| 2015-04-03 |+-----------------+1 row in set (0.00 sec)
mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');+----------------------------------------------+| DATEDIFF('2007-12-31 23:59:59','2007-12-30') |+----------------------------------------------+| 1 |+----------------------------------------------+1 row in set (0.00 sec)
mysql> select from_unixtime(unix_timestamp(),'%Y-%m-%d');+--------------------------------------------+| from_unixtime(unix_timestamp(),'%Y-%m-%d') |+--------------------------------------------+| 2015-04-03 |+--------------------------------------------+1 row in set (0.00 sec)
mysql> select HOUR(now());+-------------+| HOUR(now()) |+-------------+| 14 |+-------------+1 row in set (0.00 sec)
mysql> select last_day('2013-2-5');+----------------------+| last_day('2013-2-5') |+----------------------+| 2013-02-28 |+----------------------+1 row in set (0.00 sec)mysql> select last_day('2004-2-5');+----------------------+| last_day('2004-2-5') |+----------------------+| 2004-02-29 |+----------------------+1 row in set (0.00 sec)mysql> select last_day('2004-2-35');+-----------------------+| last_day('2004-2-35') |+-----------------------+| NULL |+-----------------------+1 row in set, 1 warning (0.00 sec)
mysql> select monthname(now());+------------------+| monthname(now()) |+------------------+| April |+------------------+1 row in set (0.00 sec)
mysql> select timestamp('2015-4-8');+-----------------------+| timestamp('2015-4-8') |+-----------------------+| 2015-04-08 00:00:00 |+-----------------------+1 row in set (0.00 sec)mysql> select timestamp('2015-4-8','10:20:30');+----------------------------------+| timestamp('2015-4-8','10:20:30') |+----------------------------------+| 2015-04-08 10:20:30 |+----------------------------------+1 row in set (0.00 sec)
unit单位:MICROSECOND、SECOND、MINUTE
, HOUR
, DAY
, WEEK
, MONTH
, QUARTER
, or YEAR
.
mysql> select timestampadd(minute,1,'2015-4-8');+-----------------------------------+| timestampadd(minute,1,'2015-4-8') |+-----------------------------------+| 2015-04-08 00:01:00 |+-----------------------------------+1 row in set (0.00 sec)mysql> select timestampadd(week,1,'2015-4-8');+---------------------------------+| timestampadd(week,1,'2015-4-8') |+---------------------------------+| 2015-04-15 |+---------------------------------+1 row in set (0.00 sec)
mysql> select timestampdiff(MONTH,'2015-4-8','2015-1-1');+--------------------------------------------+| timestampdiff(MONTH,'2015-4-8','2015-1-1') |+--------------------------------------------+| -3 |+--------------------------------------------+1 row in set (0.00 sec)mysql> select timestampdiff(MINUTE,'2015-1-1','2015-4-8');+---------------------------------------------+| timestampdiff(MINUTE,'2015-1-1','2015-4-8') |+---------------------------------------------+| 139680 |+---------------------------------------------+1 row in set (0.00 sec)
mysql> select unix_timestamp();+------------------+| unix_timestamp() |+------------------+| 1428042298 |+------------------+1 row in set (0.00 sec)
mysql> select year(now());+-------------+| year(now()) |+-------------+| 2015 |+-------------+1 row in set (0.00 sec)