Home > Database > Mysql Tutorial > How to use the DATE_ADD function in MySQL

How to use the DATE_ADD function in MySQL

王林
Release: 2023-06-03 13:43:04
forward
2190 people have browsed it

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)
Copy after login
-> 2016-01-01 00:00:00
-> 2016-01-01 23:59:59
-> 2016-01-01 00:01:00
-> 2015-12-30 14:00:00
Copy after login

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
Copy after login

Return result:

-> 2015-12-31 23:59:59
-> 2015-12-31
-> 2017-01-01 00:00:00
-> 2017-01-01 00:01:00
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template