将秒转换为人类可读的持续时间
我如何最好地将 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”。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用预定义函数 sec_to_time()
sec_to_time(number_of_seconds)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time
you can use predefined function sec_to_time()
sec_to_time(number_of_seconds)
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time
尝试这个技巧(在 MySQL 上)
SEC_TO_TIME 有时会产生错误的值
Try out this trick (on MySQL)
SEC_TO_TIME sometime produces wrong values
试试这个:(输入你的纯秒时间)
try this: (input your pure seconds time)
文档是你的朋友:
根据评论:
用法:
Documentation is your friend:
According to comment:
Usage: