Name

ADDTIME()

Synopsis

ADDTIME(datetime, datetime)

This function returns the date and time for a given string or column (in time or datetime format), incremented by the time given as the second argument. If a negative number is given, the time is subtracted. In this case, the function is the equivalent of SUBTIME(). This function is available as of version 4.1.1 of MySQL. Here is an example:

SELECT NOW( ) AS Now,
ADDTIME(NOW( ), '1:00:00.00') AS 'Hour Later';

+---------------------+---------------------+
| Now                 | Hour Later          |
+---------------------+---------------------+
| 2007-01-11 23:20:30 | 2007-01-12 00:20:30 |
+---------------------+---------------------+

Notice that the hour is increased by one, and because the time is near midnight, the function causes the date to be altered by one day as well. To increase the date, add the number of days before the time (separated by a space) like so:

SELECT NOW( ) AS Now,
ADDTIME(NOW( ), '30 0:0:0') AS 'Thirty Days Later';

+---------------------+---------------------+
| Now                 | Thirty Days Later   |
+---------------------+---------------------+
| 2007-01-11 23:20:30 | 2007-02-10 23:20:30 |
+---------------------+---------------------+

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.