Usually you do similar things in a query, but MySQL is not very good at intervals (this would be very easy with PostgreSQL). You can convert it to a unix timestamp, then it will give the number of seconds between two dates:
SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(your_datetime_column);
I was thinking about DATEDIFF, but it returns the number of days between two dates.
You can do this in PHP, for example, using the DateTime class:
$date1 = new DateTime(); $date2 = new Datetime('2010-11-26 12:00:00'); var_dump($date1->diff($date2));
(There's a procedural way to do this if you are not a fan of OOP.)
This is definitely a solution that I would use if I cannot do it using RDBMS. DateTime :: diff returns a DateInterval object that contains the number of seconds, minutes, hours, days, etc. Between two dates.
You can also do this using timestamps in PHP:
$num_sec = time() - strtotime('2010-11-26 12:00:00');
To return the same thing as the SQL query.
Vincent Savard Nov 27 '10 at 2:45 2010-11-27 02:45
source share