如何将小时数转换为天数 - MYSQL

时间:2012-12-02 19:42:56

标签: mysql

如果我有一个开始时间和结束时间,并且我找到总时间,如果超过24小时,我希望它而不是显示26,显示1天和2小时。怎么办呢?

3 个答案:

答案 0 :(得分:2)

你可能想要做一个

IF    (MOD(HOUR(TIME) / 24) )  > 24, 
    SELECT CONCAT(
    FLOOR(HOUR(TIME) / 24), ' days ',
    MOD(HOUR(TIME), 24), ' hours ',
    MINUTE(TIME), ' minutes')
)

使用Timediff

FLOOR(HOUR(TIMEDIFF('2010-01-06 08:46', '2010-01-01 12:30')) / 24), ' days ',

答案 1 :(得分:0)

如果您的start_timeend_time是日期时间类型,请使用TIMEDIFF功能并应用所需的DATE_FORMAT。

e.g。

select DATE_FORMAT(TIMEDIFF('2012:01:01 00:00:00', '2012:01:01 00:00:00.000001'),
                  '%d: %H: %m');

答案 2 :(得分:0)

这对我有用:

select
   o.id,
   o.created,
   o.complete_date,
   concat(timestampdiff(day, o.created, o.complete_date),
          'd ',
          lpad(
             mod(timestampdiff(hour, o.created, o.complete_date), 24),
             2, '0'),
          'h'
   ) as time_to_complete,
from
   orders o
;

结果:

+---------+---------------------+---------------------+------------------+
| id      | created             | complete_date       | time_to_complete |
+---------+---------------------+---------------------+------------------+
| 2393721 | 2018-02-20 00:01:44 | 2018-02-21 02:32:08 | 1d 02h           |
| 2393722 | 2018-02-20 00:02:56 | 2018-02-21 00:32:08 | 1d 00h           |
| 2393723 | 2018-02-20 00:04:12 | 2018-02-20 21:32:07 | 0d 21h           |
| 2393724 | 2018-02-20 00:04:50 | 2018-02-20 23:47:05 | 0d 23h           |
| 2393725 | 2018-02-20 00:07:07 | 2018-02-20 17:32:03 | 0d 17h           |
| 2393726 | 2018-02-20 00:13:41 | 2018-02-21 00:32:08 | 1d 00h           |
| 2393727 | 2018-02-20 00:14:25 | 2018-02-21 00:32:08 | 1d 00h           |
| 2393728 | 2018-02-20 00:16:47 | 2018-02-20 23:32:04 | 0d 23h           |
| 2393729 | 2018-02-20 00:17:19 | 2018-02-20 23:47:09 | 0d 23h           |
| 2393730 | 2018-02-20 00:17:59 | 2018-02-21 01:32:08 | 1d 01h           |
+---------+---------------------+---------------------+------------------+