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 unit date - 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 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)