mysql timediff to hours

前端 未结 10 1781
我寻月下人不归
我寻月下人不归 2020-12-13 08:47

I\'m Trying to get the timediff from my table and convert it to hours (it\'s for an hourly billed service)

SELECT TIME_TO_SEC(TIMEDIFF(endDate,startDa

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 09:27

    TIMESTAMPDIFF(HOUR, startDate, endDate)
    

    is the best way to do it, because it works with big time intervals, like

    TIMESTAMPDIFF(HOUR, "2012-01-01 00:00:00", "2050-01-01 00:00:00")
    

    Result: 333120

    while

    HOUR(TIMEDIFF("2050-01-01 00:00:00", "2012-01-01 00:00:00"))
    

    Result: 838

    fails.

    As we see in the example above, it surprisingly even works beyond the timestamp limitation in year 2038.

    The maximum hours which are returned by HOUR(TIMEDIFF(dateEnd, dateStart)) is 838, because TIMEDIFF is limited to the range allowed for TIME values.

提交回复
热议问题