MySQL将timediff输出转换为日,小时,分钟,秒格式

时间:2011-03-11 22:31:44

标签: mysql

这是我的疑问:

SELECT TIMEDIFF(end_time,start_time) AS "total" FROM `metrics`;

给了我:

116:12:10

表示116小时12分10秒。

相反,我希望它说4天20小时12分钟等等。

5 个答案:

答案 0 :(得分:28)

SELECT CONCAT(
FLOOR(HOUR(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')) / 24), ' days ',
MOD(HOUR(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')), 24), ' hours ',
MINUTE(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')), ' minutes')

在我的示例中使用end_time和start_time作为固定日期时间值

根据以下两条评论,此解决方案仅适用于35天内的日期差异。如果您知道开始和结束之间的时间超过35天,即一个月内的差异,请不要使用它。使用TIMESTAMPDIFF的其他答案将起作用。

答案 1 :(得分:13)

SELECT 
CONCAT(
TIMESTAMPDIFF(day,'2001-01-01 00:00:00','2001-01-02 23:10:00') , ' dagen ',
MOD( TIMESTAMPDIFF(hour,'2001-01-01 00:00:00','2001-01-02 23:10:00'), 24), ' uren ',
MOD( TIMESTAMPDIFF(minute,'2001-01-01 00:00:00','2001-01-02 23:10:00'), 60), ' minuten '
)

答案 2 :(得分:2)

尝试

SELECT @s:='2016-03-01' started, @e:= '2018-04-21' ended, 
FLOOR((@ms:=TIMESTAMPDIFF(MONTH,@s,@e))/12) yrs, (@ms%12) mns, 
@d:=TIMESTAMPDIFF(DAY,@s:=DATE_ADD(@s,INTERVAL @ms MONTH), @e) dys,
@hr:=TIMESTAMPDIFF(HOUR,@s:=DATE_ADD(@s, INTERVAL @d DAY),@e) hrs,
TIMESTAMPDIFF(MINUTE,TIMESTAMPADD(HOUR,@hr,@s),@e) mins;

这将为您提供两个日期之间的年,月,日,小时和分钟的细分。

希望能帮助别人。

答案 3 :(得分:1)

我使用了 Bodgans 给出的答案,但是我添加了一个条件,在数量少于10之前插入0。

CONCAT(
  IF(FLOOR(TIMESTAMPDIFF(SECOND, startDate, endDate) / 86400) < 10, '0', ''),    FLOOR(TIMESTAMPDIFF(SECOND, startDate, endDate) / 86400), ' days ',
  IF(FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 86400)/3600) < 10, '0', ''), FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 86400)/3600), ' hours ',
  IF(FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60) < 10, '0', ''), FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60), ' minutes ',
  IF((TIMESTAMPDIFF(SECOND, startDate, endDate) % 60) < 10, '0', ''), (TIMESTAMPDIFF(SECOND, startDate, endDate) % 60),  ' seconds'
)

这样,我们可以得到如下结果

  

09天10小时06分钟30秒

答案 4 :(得分:0)

最简单的方法是

CONCAT(
  FLOOR(TIMESTAMPDIFF(SECOND, startDate, endDate) / 86400), ' days ',
  FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 86400)/3600), ' hours ',
  FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60), ' minutes ',
  (TIMESTAMPDIFF(SECOND, startDate, endDate) % 60),  ' seconds'
)

要仅显示相关信息,您需要执行更复杂的版本

IF(
  FLOOR(TIMESTAMPDIFF(SECOND, startDate, endDate) / 86400) = 0,
  IF(
    FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 86400)/3600) = 0,
    IF(
      FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60) = 0,
      CONCAT((TIMESTAMPDIFF(SECOND, startDate, endDate) % 60),  ' seconds'),
      CONCAT(
        FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60), ' minutes ',
        (TIMESTAMPDIFF(SECOND, startDate, endDate) % 60),  ' seconds'
      )
    ),
    CONCAT(
      FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 86400)/3600), ' hours ',
      FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60), ' minutes ',
      (TIMESTAMPDIFF(SECOND, startDate, endDate) % 60),  ' seconds'
    )
  ),
  CONCAT(
    FLOOR(TIMESTAMPDIFF(SECOND, startDate, endDate) / 86400), ' days ',
    FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 86400)/3600), ' hours ',
    FLOOR((TIMESTAMPDIFF(SECOND, startDate, endDate) % 3600)/60), ' minutes ',
    (TIMESTAMPDIFF(SECOND, startDate, endDate) % 60),  ' seconds'
  )
)