DATEDIFF()

Description

DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

Syntax

1. > DATEDIFF(expr1,expr2)

Arguments

Arguments Description
expr1,expr2 Required. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

Examples

1. mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
2. +-------------------------------------------+
3. | datediff(2007-12-31 23:59:59, 2007-12-30) |
4. +-------------------------------------------+
5. |                                         1 |
6. +-------------------------------------------+
7. 1 row in set (0.00 sec)

9. mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
10. +-------------------------------------------+
11. | datediff(2010-11-30 23:59:59, 2010-12-31) |
12. +-------------------------------------------+
13. |                                       -31 |
14. +-------------------------------------------+
15. 1 row in set (0.00 sec)
1. create table t1(a INT,  b date);
2. insert into t1 values(1, "2012-10-11");
3. insert into t1 values(2, "2004-04-24");
4. insert into t1 values(3, "2008-12-04");
5. insert into t1 values(4, "2012-03-23");
6. insert into t1 values(5, "2000-03-23");
7. insert into t1 values(6, "2030-03-23");
8. insert into t1 values(7, "2040-03-23");

10. mysql> SELECT a, DATEDIFF('2022-10-9', b) from t1;
11. +------+------------------------+
12. | a    | datediff(2022-10-9, b) |
13. +------+------------------------+
14. |    1 |                   3650 |
15. |    2 |                   6742 |
16. |    3 |                   5057 |
17. |    4 |                   3852 |
18. |    5 |                   8235 |
19. |    6 |                  -2722 |
20. |    7 |                  -6375 |
21. +------+------------------------+
22. 7 rows in set (0.01 sec)