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

问题描述 投票:1回答:3

如果我有一个开始时间和结束时间,并且我找到总时间,如果超过24小时,我希望它而不是显示26,显示1天和2小时。如何才能做到这一点?

mysql
3个回答
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 ',

0
投票

如果你的start_timeend_time是日期时间类型,那么使用TIMEDIFF函数并应用所需的DATE_FORMAT。

EG

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

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           |
+---------+---------------------+---------------------+------------------+
© www.soinside.com 2019 - 2024. All rights reserved.