date_diff
Descriptionβ
Returns the difference between two date values in the specified unit. This function returns the value of expr1 - expr2 expressed in terms of unit.
For example, date_diff('second', '2010-11-30 23:59:59', '2010-11-30 20:58:59') returns the difference between the two DATETIME values in unit of seconds.
This function is supported from v3.1.
The difference between date_diff and datediff lies in that datediff() does not support the unit parameter.
Syntaxβ
BIGINT DATE_DIFF(VARCHAR unit, DATETIME expr1, DATETIME expr2)
Parametersβ
-
expr1andexpr2: the two datetime values you want to compare, required. Supported data types are DATETIME and DATE. -
unit: the unit used to express the time difference, required. The followingunitvalues are supported: year, quarter, month, week, day, hour, minute, second, millisecond.
Return valueβ
Returns a BIGINT value.
Usage notesβ
- If
expr1is earlier thanexpr2, a negative value is returned. - If
unitis invalid, an error is returned. - If any input value is NULL, NULL is returned.
- If the specified date does not exist, for example,
2022-11-31, NULL is returned.
Examplesβ
mysql> select date_diff('second', '2010-11-30 23:59:59', '2010-11-30 20:58:59');
+-------------------------------------------------------------------+
| date_diff('second', '2010-11-30 23:59:59', '2010-11-30 20:58:59') |
+-------------------------------------------------------------------+
| 10860 |
+-------------------------------------------------------------------+
mysql> select date_diff('minute', '2010-11-30 23:59:59', '2010-11-30 20:58:59');
+-------------------------------------------------------------------+
| date_diff('minute', '2010-11-30 23:59:59', '2010-11-30 20:58:59') |
+-------------------------------------------------------------------+
| 181 |
+-------------------------------------------------------------------+
mysql> select date_diff('hour', '2010-11-30 23:59:59', '2010-11-30 20:58:59');
+-----------------------------------------------------------------+
| date_diff('hour', '2010-11-30 23:59:59', '2010-11-30 20:58:59') |
+-----------------------------------------------------------------+
| 3 |
+-----------------------------------------------------------------+
mysql> select date_diff('millisecond', '2010-11-30 23:59:59', '2010-11-30 20:58:59');
+------------------------------------------------------------------------+
| date_diff('millisecond', '2010-11-30 23:59:59', '2010-11-30 20:58:59') |
+------------------------------------------------------------------------+
| 10860000 |
+------------------------------------------------------------------------+