MySQL DATE_ADD() Function
The DATE_ADD()
function in MySQL lets you easily add or subtract a time interval to or from a date. This is invaluable for calculating future or past dates, creating date ranges, and generally manipulating temporal data within your MySQL queries.
DATE_ADD(): Definition and Usage
DATE_ADD()
provides a flexible way to adjust dates. You specify a starting date, the amount to add or subtract, and the unit of time (days, months, years, hours, minutes, etc.). Using a negative value for the amount will effectively subtract the time interval.
Syntax
Syntax
DATE_ADD(date, INTERVAL value unit)
Parameter Values
Parameter | Description |
---|---|
date |
The starting date or datetime value. This is required. |
value |
The number of intervals to add (positive) or subtract (negative). This is required. |
unit |
The unit of time. Options: MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , YEAR , SECOND_MICROSECOND , MINUTE_MICROSECOND , MINUTE_SECOND , HOUR_MICROSECOND , HOUR_SECOND , HOUR_MINUTE , DAY_MICROSECOND , DAY_SECOND , DAY_MINUTE , DAY_HOUR , YEAR_MONTH . This is required. |
Examples
Adding Days to a Date
This example adds 10 days to June 15th, 2017.
Syntax
SELECT DATE_ADD("2017-06-15", INTERVAL 10 DAY);
Output
2017-06-25
Adding Minutes to a Datetime
Adding 15 minutes to a datetime value.
Syntax
SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL 15 MINUTE);
Output
2017-06-15 09:49:21
Subtracting Hours from a Datetime
Subtracting 3 hours from a datetime value.
Syntax
SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL -3 HOUR);
Output
2017-06-15 06:34:21
Subtracting Months from a Date
Subtracting 2 months from a date.
Syntax
SELECT DATE_ADD("2017-06-15", INTERVAL -2 MONTH);
Output
2017-04-15