Only show hours in MYSQL DATEDIFF

♀尐吖头ヾ 提交于 2019-11-27 22:41:53
FlyingMolga

What about using TIMESTAMPDIFF?

SELECT TIMESTAMPDIFF(HOUR, start_time, end_time) 
           as `difference` FROM timeattendance WHERE timeattendance_id = '1484'

You better use TIMEDIFF instead of DATEDIFF since DATEDIFF casts all times to dates before comparing. After performing TIMEDIFF you can obtain hours with HOUR function.

SELECT HOUR(TIMEDIFF(end_time, start_time)) as `difference`;

As stated in MySQL reference, DATEDIFF only takes days in account.

If both dates are after 1970, you can substract timestamps:

SELECT ( UNIX_TIMESTAMP("2012-01-02 13:00:00") - 
         UNIX_TIMESTAMP("2012-01-01 12:00:00") ) / 3600 ...

or:

SELECT TIMEDIFF ( "2012-01-02 13:00:00", "2012-01-01 12:00:00") / 3600 ... 
jth_92

You can use the TIMEDIFF and HOUR function to just extract the hour.

SELECT HOUR(TIMEDIFF(end_time, start_time)) as `difference` FROM timeattendance WHERE timeattendance_id = '1484'

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!