【发布时间】:2012-01-01 21:25:35
【问题描述】:
如何最好地将 90060(秒)转换为“25h 1m”字符串?
目前我在 SQL 中这样做:
SELECT
IF(
HOUR(
sec_to_time(
sum(time_to_sec(task_records.time_spent))
)
) > 0,
CONCAT(
HOUR(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
'h ',
MINUTE(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
'm'
),
CONCAT(
MINUTE(sec_to_time(sum(time_to_sec(task_records.time_spent)))),
'm'
)
) as time
FROM myTable;
但我不确定这是不是最方便的方法 :-)
我愿意接受有关在 SQL(与我已经在做的不同)或 PHP 中执行此操作的建议。
编辑:
所需字符串的示例:“5m”、“40m”、“1h 35m”、“45h”、“46h 12m”。
【问题讨论】:
-
不应该 90060 是
1d 1h 1min而不是25h 1min吗? -
不,我需要严格按小时计算。
-
警告接受的答案对于大于 3020399 秒(SEC_TO_TIME 正确转换的最大值)的值失败。也许 OP 的任务永远不会运行超过 839 小时,但这绝对不是通用解决方案。