Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate interval between datetime in MySQL?

Tags:

sql

mysql

For example,how to calculate the interval between these two datetime:

2009-09-18 00:00:00

2009-10-17 00:00:00

EDIT

I mean to get the interval in the format of year-month-day hour:min:seconds

like image 581
omg Avatar asked Sep 22 '09 05:09

omg


2 Answers

What about using datediff :

mysql> select abs(datediff('2009-09-18 00:00:00', '2009-10-17 00:00:00'));
+-------------------------------------------------------------+
| abs(datediff('2009-09-18 00:00:00', '2009-10-17 00:00:00')) |
+-------------------------------------------------------------+
|                                                          29 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

Quoting the manual :

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.

like image 165
Pascal MARTIN Avatar answered Nov 01 '22 12:11

Pascal MARTIN


You may try out DATEDIFF or TIMEDIFF

like image 5
Janis Veinbergs Avatar answered Nov 01 '22 12:11

Janis Veinbergs