SUBTIME( )

This MySQL function returns the date and time for the given string or column, decreased by the time given as the second argument (d hh:mm:ss).

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

SUBTIME(datetime, datetime_value)

Explanation

This function returns the date and time for the given string or column, decreased by the time given as the second argument (d hh:mm:ss). If a negative number is given, the time is added and the function is the equivalent of ADDTIME( ). This function is available as of version 4.1.1 of MySQL.

Examples

SELECT NOW( ) AS Now,
SUBTIME(NOW( ), '1:00:00.000000') AS 'Hour Ago';

+---------------------+---------------------+
| Now                 | Hour Ago            |
+---------------------+---------------------+
| 2008-01-12 00:54:59 | 2008-01-11 23:54:59 |
+---------------------+---------------------+

Notice that the hour was decreased by one, and because the time is just after midnight, the function causes the date to be altered by one day, as well. If either argument is given with a microsecond value other than all zeros, the results will include microseconds. To decrease the date, give the number of days before the time (separated by a space) like so:

SELECT NOW( ) AS Now,
SUBTIME(NOW( ), '30 0:0.0') AS 'Thirty Days Ago';

+---------------------+---------------------+
| Now                 | Thirty Days Ago     |
+---------------------+---------------------+
| 2008-01-12 00:57:04 | 2007-12-13 00:57:04 |
+---------------------+---------------------+