以天为单位格式化TIMEDIFF的结果:小时:分钟:秒

时间:2012-08-19 07:35:15

标签: mysql sql datetime-format

  

可能重复:
  MySQL convert timediff output to day, hour, minute, second format

我有一个简单的查询,用于计算两个日期时间之间的时差,如下所示:

 SELECT TIMEDIFF( '2012-08-19 08:25:13', '2012-07-19 18:05:52' );
  

输出:734:19:21

输出以小时为单位:分钟:秒。但是我想将输出格式化为:

  

天:小时:分:秒

如何格式化输出?

2 个答案:

答案 0 :(得分:5)

SELECT CONCAT(
   FLOOR(HOUR(TIMEDIFF('2012-08-05 09:56', '2012-08-02 11:20')) / 24), ' days, ',
   MOD(HOUR(TIMEDIFF('2012-08-05 09:56', '2012-08-02 11:20')), 24), ' hours, ',
   MINUTE(TIMEDIFF('2012-08-05 09:56', '2012-08-02 11:20')), ' minutes, ',
   SECOND(TIMEDIFF('2012-08-05 09:56', '2012-08-02 11:20')), ' seconds')
AS TimeDiff

See this fiddle

答案 1 :(得分:0)

SELECT date_format(from_unixtime(unix_timestamp('2012-08-19 08:25:13' ) -
unix_timestamp('2012-07-19 18:05:52' )), '%d.%m.%Y %H:%i:%s');