官方文檔:Date and Time Functionshtml
Name | Description |
---|---|
ADDDATE() |
Add time values (intervals) to a date value |
ADDTIME() |
Add time |
CONVERT_TZ() |
Convert from one timezone to another |
CURDATE() |
Return the current date |
CURRENT_DATE() , CURRENT_DATE |
Synonyms for CURDATE() |
CURRENT_TIME() , CURRENT_TIME |
Synonyms for CURTIME() |
CURRENT_TIMESTAMP() , CURRENT_TIMESTAMP |
Synonyms for NOW() |
CURTIME() |
Return the current time |
DATE() |
Extract the date part of a date or datetime expression |
DATE_ADD() |
Add time values (intervals) to a date value |
DATE_FORMAT() |
Format date as specified |
DATE_SUB() |
Subtract a time value (interval) from a date |
DATEDIFF() |
Subtract two dates |
DAY() |
Synonym for DAYOFMONTH() |
DAYNAME() |
Return the name of the weekday |
DAYOFMONTH() |
Return the day of the month (0-31) |
DAYOFWEEK() |
Return the weekday index of the argument |
DAYOFYEAR() |
Return the day of the year (1-366) |
EXTRACT() |
Extract part of a date |
FROM_DAYS() |
Convert a day number to a date |
FROM_UNIXTIME() |
Format UNIX timestamp as a date |
GET_FORMAT() |
Return a date format string |
HOUR() |
Extract the hour |
LAST_DAY |
Return the last day of the month for the argument |
LOCALTIME() , LOCALTIME |
Synonym for NOW() |
LOCALTIMESTAMP , LOCALTIMESTAMP() |
Synonym for NOW() |
MAKEDATE() |
Create a date from the year and day of year |
MAKETIME() |
Create time from hour, minute, second |
MICROSECOND() |
Return the microseconds from argument |
MINUTE() |
Return the minute from the argument |
MONTH() |
Return the month from the date passed |
MONTHNAME() |
Return the name of the month |
NOW() |
Return the current date and time |
PERIOD_ADD() |
Add a period to a year-month |
PERIOD_DIFF() |
Return the number of months between periods |
QUARTER() |
Return the quarter from a date argument |
SEC_TO_TIME() |
Converts seconds to 'HH:MM:SS' format |
SECOND() |
Return the second (0-59) |
STR_TO_DATE() |
Convert a string to a date |
SUBDATE() |
Synonym for DATE_SUB() when invoked with three arguments |
SUBTIME() |
Subtract times |
SYSDATE() |
Return the time at which the function executes |
TIME() |
Extract the time portion of the expression passed |
TIME_FORMAT() |
Format as time |
TIME_TO_SEC() |
Return the argument converted to seconds |
TIMEDIFF() |
Subtract time |
TIMESTAMP() |
With a single argument, this function returns the date or datetime expression; with two arguments, the sum of the arguments |
TIMESTAMPADD() |
Add an interval to a datetime expression |
TIMESTAMPDIFF() |
Subtract an interval from a datetime expression |
TO_DAYS() |
Return the date argument converted to days |
TO_SECONDS() |
Return the date or datetime argument converted to seconds since Year 0 |
UNIX_TIMESTAMP() |
Return a UNIX timestamp |
UTC_DATE() |
Return the current UTC date |
UTC_TIME() |
Return the current UTC time |
UTC_TIMESTAMP() |
Return the current UTC date and time |
WEEK() |
Return the week number |
WEEKDAY() |
Return the weekday index |
WEEKOFYEAR() |
Return the calendar week of the date (1-53) |
YEAR() |
Return the year |
YEARWEEK() |
Return the year and week |
注:mysql
CONVERT_TZ(dt,from_tz,to_tz):時區轉換sql
mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET'); -> '2004-01-01 13:00:00' mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00'); -> '2004-01-01 22:00:00'
CURDATE()/CURRENT_DATE()express
返回當前的日期。'YYYY-MM-DD' or YYYYMMDD 形式,根據須要。函數
mysql> SELECT CURDATE(); -> '2008-06-13' mysql> SELECT CURDATE() + 0; -> 20080613
CURTIME()/CURRENT_TIME()this
返回當前的時間。'HH:MM:SS' or HHMMSS 形式,根據須要。spa
mysql> SELECT CURTIME(); -> '23:50:26' mysql> SELECT CURTIME() + 0; -> 235026.000000
DATE(str):抽取date或datetime參數的日期部分。unix
TIME(str):抽取time或datetime參數的時間部分。code
mysql> SELECT DATE('2003-12-31 01:02:03'); -> '2003-12-31' mysql> SELECT TIME('2003-12-31 01:02:03'); -> '01:02:03' mysql> SELECT TIME('2003-12-31 01:02:03.000123'); -> '01:02:03.000123'
DATEDIFF(date1,date2)orm
DATEDIFF() 函數返回兩個日期之間的天數(只比天),date1 和 date2 參數是合法的datetime/date表達式。
select datediff('2016-03-29','2016-03-29'); select datediff('2016-03-29 00:00:00','2016-03-29 23:59:59');
TIMEDIFF(date1,date2)
TIMEDIFF()函數返回兩個日期之間的時分秒數(HH:MM:ss),date1 和 date2 參數是datetime/time表達式。
select timediff('2016-03-30 00:00:00','2016-03-28 11:11:11'); select timediff('00:00:00','11:11:11');
DATE_SUB(date,INTERVAL expr type)
date 參數是合法的日期表達式。expr 參數是您但願添加的時間間隔。
SELECT id FROM my_table WHERE create_time >= date_sub(now(), INTERVAL 3 HOUR) AND create_time < now();
Type 值
當咱們在給now()+-一個時間的時候,其實應該這樣理解的:
+1/+01:加1秒鐘
+101/+0101:加1分鐘1秒鐘
+10101/+010101:加1小時1分鐘1秒鐘
+1010101/+01010101:加1天1時1分鐘1秒鐘
+101010101/+0101010101:加1月1天1時1分鐘1秒鐘
+1101010101/+010101010101:加1年1月1天1時1分鐘1秒鐘,這裏要注意下,年這個部分能夠是4位(高位沒有的話會補零):00010101010101
DATE_FORMAT(date,format)
用於以不一樣的格式顯示日期/時間數據。
SELECT DATE_FORMAT(insert_time,'%Y-%m-%d %H:%i:%S') AS insert_time FROM user; SELECT DATE_FORMAT(insert_time,'%Y-%m-%d') AS day, COUNT(id) AS count FROM user GROUP BY day;
格式 | 描述 |
%a | 縮寫星期名 |
%b | 縮寫月名 |
%c | 月,數值 |
%D | 帶有英文前綴的月中的天 |
%d | 月的天,數值(00-31) |
%e | 月的天,數值(0-31) |
%f | 微秒 |
%H | 小時 (00-23) |
%h | 小時 (01-12) |
%I | 小時 (01-12) |
%i | 分鐘,數值(00-59) |
%j | 年的天 (001-366) |
%k | 小時 (0-23) |
%l | 小時 (1-12) |
%M | 月名 |
%m | 月,數值(00-12) |
%p | AM 或 PM |
%r | 時間,12-小時(hh:mm:ss AM 或 PM) |
%S | 秒(00-59) |
%s | 秒(00-59) |
%T | 時間, 24-小時 (hh:mm:ss) |
%U | 周 (00-53) 星期日是一週的第一天 |
%u | 周 (00-53) 星期一是一週的第一天 |
%V | 周 (01-53) 星期日是一週的第一天,與 %X 使用 |
%v | 周 (01-53) 星期一是一週的第一天,與 %x 使用 |
%W | 星期名 |
%w | 周的天 (0=星期日, 6=星期六) |
%X | 年,其中的星期日是周的第一天,4 位,與 %V 使用 |
%x | 年,其中的星期一是周的第一天,4 位,與 %v 使用 |
%Y | 年,4 位 |
%y | 年,2 位 |
DAYOFWEEK(date):返回date所表明的一星期中的第幾天(1~7)
DAYOFMONTH(date):返回date是一個月的第幾天(1~31)
DAYOFYEAR(date):返回date是一年的第幾天(1~366)
MINUTE(time):返回time的分鐘值(0~59)
HOUR(time):返回time的小時值(0~23)
DAY(date):返回date是一個月的第幾天(1~31),等同於DAYOFMONTH(date)
WEEK(date):返回日期date爲一年中第幾周(0~53)
MONTH(date):返回date的月份值(1~12)
QUARTER(date):返回date在一年中的季度(1~4)
YEAR(date):返回日期date的年份(1000~9999)
DAYNAME(date): 返回date的星期名
MONTHNAME(date):返回date的月份名
EXTRACT(unit FROM date)
從時間裏抽取對應的單位。unit參數DATE_SUB的Type。
mysql> SELECT EXTRACT(YEAR FROM '2009-07-02'); -> 2009 mysql> SELECT EXTRACT(YEAR_MONTH FROM '2009-07-02 01:02:03'); -> 200907 mysql> SELECT EXTRACT(DAY_MINUTE FROM '2009-07-02 01:02:03'); -> 20102 mysql> SELECT EXTRACT(MICROSECOND -> FROM '2003-01-02 10:30:00.000123'); -> 123
FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format)
將unix_timestamp類型的參數轉換爲'YYYY-MM-DD HH:MM:SS' 或 YYYYMMDDHHMMSS 形式。若是給定fromat,則按指定格式轉。
mysql> SELECT FROM_UNIXTIME(1447430881); -> '2015-11-13 10:08:01' mysql> SELECT FROM_UNIXTIME(1447430881) + 0; -> 20151113100801 mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), -> '%Y %D %M %h:%i:%s %x'); -> '2015 13th November 10:08:01 2015'
LAST_DAY(date):返回當月的最後一天(date類型)。date非法,則返回NULL。
mysql> SELECT LAST_DAY('2003-02-05'); -> '2003-02-28' mysql> SELECT LAST_DAY('2004-02-05'); -> '2004-02-29' mysql> SELECT LAST_DAY('2004-01-01 01:01:01'); -> '2004-01-31' mysql> SELECT LAST_DAY('2003-03-32'); -> NULL
NOW():返回當前的日期和時間。'YYYY-MM-DD HH:MM:SS' 或者 YYYYMMDDHHMMSS形式,根據須要。它記錄的是SQL語句開始執行的時間,因此一條語句有多個NOW()時,其返回結果是同樣的。
SYSDATE():返回當前的日期和時間。'YYYY-MM-DD HH:MM:SS' 或者 YYYYMMDDHHMMSS形式,根據須要。它記錄的是SYSDATE()函數開始執行的時間,因此一條語句有多個SYSDATE()時,其返回結果是不同的。
參考: