Home  >  Article  >  Database  >  How to use the DATE_ADD function in MySQL

How to use the DATE_ADD function in MySQL

王林
王林forward
2023-06-03 13:43:042145browse

DATE_ADD(date, INTERVAL number type), same as ADDDATE()

SELECT DATE_ADD(“2015-12-31 23:59:59”,INTERVAL 1 SECOND) 
SELECT DATE_ADD(“2015-12-31 23:59:59”,INTERVAL 1 DAY) 
SELECT DATE_ADD(“2015-12-31 23:59:59”,INTERVAL “1:1” MINUTE_SECOND) 
SELECT DATE_ADD(“2016-01-01 00:00:00”,INTERVAL “-1 10” DAY_HOUR)
-> 2016-01-01 00:00:00
-> 2016-01-01 23:59:59
-> 2016-01-01 00:01:00
-> 2015-12-30 14:00:00

DATE_ADD() and ADDDATE() return the result of date operation

1, The format of date can be "15-12-31", "15-12-31 23:59:59", or "2015-12-31 23:59:59", if the parameter date is in date format , then the date format result is returned. If the parameter date is in datetime format, the datetime format result is returned

2. type format:
SECOND seconds SECONDS
MINUTE minutes MINUTES
HOUR time HOURS
DAY DAYS
MONTH Month MONTHS
YEAR YEARS
MINUTE_SECOND Minutes and seconds "MINUTES:SECONDS"
HOUR_MINUTE Hours and minutes "HOURS:MINUTES"
DAY_HOUR Days and hours "DAYS HOURS "
YEAR_MONTH years and months"YEARS-MONTHS"
HOUR_SECOND hours, minutes, "HOURS:MINUTES:SECONDS"
DAY_MINUTE days, hours, minutes"DAYS HOURS:MINUTES"
DAY_SECOND days, hours , minutes, seconds "DAYS HOURS:MINUTES:SECONDS"


3. In addition, if you do not use a function, you can also consider using the operators " ", "-", the example is as follows:

SELECT “2016-01-01” - INTERVAL 1 SECOND 
SELECT “2016-01-01” - INTERVAL 1 DAY 
SELECT ‘2016-12-31 23:59:59' + INTERVAL 1 SECOND 
SELECT ‘2016-12-31 23:59:59' + INTERVAL “1:1” MINUTE_SECOND

Return result:

-> 2015-12-31 23:59:59
-> 2015-12-31
-> 2017-01-01 00:00:00
-> 2017-01-01 00:01:00

The above is the detailed content of How to use the DATE_ADD function in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete