MySQL Calculate Hours From Two Timestamps

To calculate the difference between two timestamps in seconds or hours use the following:
SELECT (
    UNIX_TIMESTAMP("2009-08-17 10:00") - 
    UNIX_TIMESTAMP("2009-08-17 09:00")
) AS seconds
>>> 3600
SELECT (
    (UNIX_TIMESTAMP("2009-08-17 10:00") -
     UNIX_TIMESTAMP("2009-08-17 09:00")) / 3600
) AS hours
>>> 1.0000
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment