SUBTIME()
SUBTIME(datetime,datetime_value)
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. Here is an example:
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 is 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 | +---------------------+---------------------+