TIMESTAMPDIFF( )

This MySQL function returns the time difference between the two times given, but only for the interval being compared.

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).

TIMESTAMPDIFF(interval, datetime, datetime)

Explanation

This function returns the time difference between the two times given, but only for the interval being compared. The intervals accepted are the same as those for TIMESTAMPADD( ). This function is available as of version 5.0.0 of MySQL.

Examples

SELECT NOW( ) AS Today,
TIMESTAMPDIFF(DAY, NOW( ), LAST_DAY(NOW( )))
AS 'Days Remaining in Month';

+---------------------+-------------------------+
| Today               | Days Remaining in Month |
+---------------------+-------------------------+
| 2008-01-12 02:19:26 | 19                      |
+---------------------+-------------------------+

This SQL statement retrieves the current date and time and uses the LAST_DAY( ) function to determine the date of the last day of the month. Then TIMESTAMPDIFF( ) function determines the difference between the day of the date now and the day for the date at the end of the month.