MySQL to calculate the number of days between two dates, months, years

MySQL built-in date functions TIMESTAMPDIFF calculate difference between two dates seconds, minutes, hours, days, weeks, quarters, months, years, increase or decrease the current date day, a week, and so on.

SELECT TIMESTAMPDIFF (type, start time, end time)

The difference between the number of seconds:

SELECT TIMESTAMPDIFF(SECOND,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

The difference between the number of minutes:

SELECT TIMESTAMPDIFF(MINUTE,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

The difference between the number of hours:

SELECT TIMESTAMPDIFF(HOUR,'1993-03-23 00:00:00 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

The difference between the number of days:

SELECT TIMESTAMPDIFF(DAY,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

The difference between the number of weeks:

SELECT TIMESTAMPDIFF(WEEK,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

The difference between the number of quarters:

SELECT TIMESTAMPDIFF(QUARTER,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

A difference of a few months:

SELECT TIMESTAMPDIFF(MONTH,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

A difference of a few years:

SELECT TIMESTAMPDIFF(YEAR,'1993-03-23 00:00:00',DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))

Get the current date:

SELECT NOW()
SELECT CURDATE()

Add a day to the current date:

SELECT DATE_SUB(CURDATE(),INTERVAL -1 DAY)

The current date to reduce the Day:

SELECT DATE_SUB(CURDATE(),INTERVAL 1 DAY)

The current date for an additional week:

SELECT DATE_SUB(CURDATE(),INTERVAL -1 WEEK)

SELECT DATE_SUB(NOW(),INTERVAL -1 MONTH)

The current date increase in January:

SELECT DATE_SUB(CURDATE(),INTERVAL -1 MONTH)
  • FRAC_SECOND ms
  • SECOND sec
  • MINUTE minutes
  • HOUR hours
  • DAY days
  • WEEK week
  • MONTH month
  • QUARTER quarter
  • , YEAR

Guess you like

Origin www.cnblogs.com/i-tao/p/11419900.html