Home >Database >Mysql Tutorial >Summary of date functions in MySQL
Regarding the MySQL date and time functions, I can always find some new features every time, so I simply make a unified arrangement and supplement the note library. There may be some omissions, welcome to add.
This article has compiled 30 time and date functions, divided into 4 paragraphs for analysis, see below for details.
Get the current time
sysdate()
now(), current_timestamp(), current_timestamp, LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, LOCALTIMESTAMP()
rest ns
sleep(n)
Example
Sameness: Get the current time
Difference: When is the returned time, the time value of executing SQL or calling the function
sysdate(), returns the time value at the time when the function is called
now (), current_timestamp()/current_timestamp, LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, LOCALTIMESTAMP() returns the time value when the statement is called;
Please clear it clearly during use The difference between these three
now(), current_timestamp(), current_timestamp, LOCALTIME, LOCALTIME() , LOCALTIMESTAMP , LOCALTIMESTAMP()
Time difference
##datediff(date1, date2), timediff(time1, time2), timestampdiff(interval,datetime_expr1,datetime_expr2)
##date_sub(date, INTERVAL expr type)
DATE_ADD(date,INTERVAL expr type)
interval can be:
FRAC_SECOND milliseconds, SECOND seconds, MINUTE minutes , HOUR hours
DAY days, WEEK weeks, MONTH months, QUARTER quarters, YEAR years
The difference time of subtracting two times, note that it needs to be in the same format
two Subtract the dates to get the number of days. Note that the two must be in the same format
##datediff(date1, date2)
Date minus the specified time interval.
Type Value | |||
---|---|---|---|
One-tenth of a second, ±n | date_sub( @date ,INTERVAL 2 MICROSECOND ) | 2017-09-09 09:09 | :08.999998 |
seconds | date_sub( @date ,INTERVAL 2 SECOND ) | 2017-09-09 09:09: | 07 |
minutes | date_sub( @date ,INTERVAL 2 MINUTE ) | 2017-09-09 09: | 07:09 |
hour | date_sub( @date ,INTERVAL 2 HOUR) | 2017-09-09 | 07:07:09 | ##DAY
date_sub( @date ,INTERVAL 2 DAY ) | 2017-09- | 07 | 09:09:09WEEK |
date_sub( @date ,INTERVAL 2 WEEK ) | 2017- | 08-26 | 09:09:09MONTH |
date_sub( @date ,INTERVAL 2 MONTH ) | 2017- | 07 | -09 09: 09:09QUARTER(Quarter, 3 months) |
date_sub( @date ,INTERVAL 2 QUARTER ) | 2017- | 03 | -09 09:09:09YEAR |
date_sub( @date ,INTERVAL 2 YEAR ) | 2015 | -09-09 09:09:09SECOND_MICROSECOND | |
date_sub( @date ,INTERVAL '2.2' SECOND_MICROSECOND ) | 2017-09-09 09:09: | 06.980000 | MINUTE_MICROSECOND |
date_sub( @date ,INTERVAL '2:2.2' MINUTE_MICROSECOND ) | 2017-09 -09 09: | 07:06.800000 | MINUTE_SECOND |
date_sub( @date , INTERVAL '2:2' MINUTE_SECOND ) | 2017-09-09 09: | 07:07 | HOUR_MICROSECOND |
date_sub( @date ,INTERVAL '2:2:2.2' HOUR_MICROSECOND ) | 2017-09-09 | 07:07:06.800000 | HOUR_SECOND |
date_sub( @date ,INTERVAL '2:2:2' HOUR_SECOND ) | 2017-09-09 | 07:07:07 | HOUR_MINUTE |
date_sub( @date ,INTERVAL '2:2' HOUR_MINUTE ) | 2017-09-09 | 07:07 | :09##DAY_MICROSECOND |
date_sub( @date ,INTERVAL '2 2:2:2.2' DAY_MICROSECOND ) | 2017-09- | 07 07:07:06.800000DAY_SECOND | |
date_sub( @date ,INTERVAL ' 2 2:2:2' DAY_SECOND ) | 2017-09- | 07 07:07:07DAY_MINUTE | |
date_sub( @date ,INTERVAL '2 2:2 ' DAY_MINUTE ) | 2017-09- | 07 07:07:09 | DAY_HOUR |
date_sub( @date ,INTERVAL '2 2' DAY_HOUR ) | 2017 -09- | 07 07:09:09 | YEAR_MONTH |
date_sub( @ date ,INTERVAL '2 2' YEAR_MONTH ) | 2015-07 | -09 09:09:09 |
to_days (datetime), dayofyear(datetime), dayofmonth(datetime)
|
The above is the detailed content of Summary of date functions in MySQL. For more information, please follow other related articles on the PHP Chinese website!