The following table lists the most important built-in date functions in MySQL:
function |
Description |
Now () |
Returns the current date and time |
Curdate () |
Returns the current date |
Curtime () |
Returns the current time |
DATE () |
Extract date part of date or date/time expression |
EXTRACT () |
Returns a separate part of the date/time Press |
Date_add () |
Add a specified time interval to a date |
Date_sub () |
Subtract a specified time interval from a date |
DATEDIFF () |
Returns the number of days between two dates |
Date_format () |
Display Date/time in a different format |
Time difference function (Timestampdiff, DATEDIFF)
Need to use MySQL to calculate the time difference, using Timestampdiff, DATEDIFF, record the results of the experiment
--0Select DateDiff(now (), now ());--2Select DateDiff('2015-04-22 23:59:00','2015-04-20 00:00:00');--2Select DateDiff('2015-04-22 00:00:00','2015-04-20 23:59:00');--1SelectTimestampdiff ( Day,'2015-04-20 23:59:00','2015-04-22 00:00:00');--2SelectTimestampdiff ( Day,'2015-04-20 00:00:00','2015-04-22 00:00:00');--2SelectTimestampdiff ( Day,'2015-04-20 00:00:00','2015-04-22 12:00:00');--2SelectTimestampdiff ( Day,'2015-04-20 00:00:00','2015-04-22 23:59:00');-- inSelectTimestampdiff (HOUR,'2015-04-20 00:00:00','2015-04-22 23:00:00');--4260SelectTimestampdiff (MINUTE,'2015-04-20 00:00:00','2015-04-22 23:00:00');
Date conversion calculation functions (Date_add, Day, Date_format, str_to_date)
--use date and string conversions to calculate the first day of the month, the first day of the next monthSelectCurdate () as 'Current Date', Date_format (Curdate (),'%y-%m') as 'Current Month', Str_to_date (Concat (Date_format (Curdate (),'%y-%m'),'-01'),'%y-%m-%d') as 'the first day of the current month', Date_add (Str_to_date (Concat (Date_format) (Curdate (),'%y-%m'),'-01'),'%y-%m-%d'), Interval1 Month) as 'the first day of next month';--the last day of the current monthSelectLast_day (Curdate ());--the first day of next monthSelectDate_add (Last_day (Curdate ()), interval1 Day);--the day of the monthSelect Day(Curdate ());--first day of the monthSelectDate_add (Curdate (), Interval1-( Day(Curdate ())) Day);
MySQL built-in date processing function