When dealing with date and time values, one of the more common (and complex) tasks involves performing addition and subtraction operations on these values. However, with MySQL's powerful date and time API taking care of all the minor adjustments for you, manipulating date and time data is no longer the tedious and time-consuming process it used to be. Find out why, inside.
It should be noted that MySQL includes intelligence to automatically format the result of the calculation as either a date-only or date-and-time value. For example, if your arguments to DATE_ADD() or DATE_SUB() contains only year, month and day components, MySQL will output a date-only value as result.
However, if your arguments include a time component - hours, minutes or seconds - then MySQL produces a result containing both date and time components.
MySQL also provides an alternative syntax to DATE_ADD() and DATE_SUB(), which is sometimes more readable - this involves using + and - signs to indicate the type of calculation to be performed. Consider the following examples, which illustrate:
mysql> SELECT 20000615 + INTERVAL 5 DAY;
+---------------------------+
| 20000615 + INTERVAL 5 DAY |
+---------------------------+
| 2000-06-20 |
+---------------------------+
1 row in set (0.00 sec)
mysql> SELECT 20000615000000 - INTERVAL 10 SECOND ;
+-------------------------------------+
| 20000615000000 - INTERVAL 10 SECOND |
+-------------------------------------+
| 2000-06-14 23:59:50 |
+-------------------------------------+
1 row in set (0.00 sec)