How to get the difference between two dates rounded to hours

Viewed 97730

I'll illustrate what I would like to get in the following example:

'2010-09-01 03:00:00' - '2010-09-01 00:10:00'

Using TIMEDIFF(), we get 2 as a result. This means, it's not considering the 50 minutes left.

In this case, what I'd like to get is: 50 (minutes) / 60 = 0.83 period. Therefore, the result should be 2.83 and not 2.

9 Answers
SELECT ABS(TIME_TO_SEC(TIMEDIFF('2010-09-01 03:00:00', '2010-09-01 00:10:00')) / 3600)

Result: 2.8333

Using UNIX_TIMESTAMP:

SELECT (UNIX_TIMESTAMP(End)-UNIX_TIMESTAMP(Start))/3600
AS Difference_in_hours 
FROM Table
Related