如何在 MySQL 中将时间以秒为单位转换为 HH:MM:SS 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18911455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert time in seconds to HH:MM:SS format in MySQL?
提问by PHPLover
I'm having a data column named test_duration bigint(12). I'm storing time in seconds into the database. Now when I fetch record from the table I want the time converted into HH:MM:SS format. How should I achieve this? Thanks in advance.
我有一个名为test_duration bigint(12)的数据列。我将时间以秒为单位存储到数据库中。现在,当我从表中获取记录时,我希望将时间转换为 HH:MM:SS 格式。我应该如何实现这一目标?提前致谢。
回答by Subedi Kishor
You can use MySQLfunction SEC_TO_TIME().
您可以使用MySQL函数SEC_TO_TIME()。
Example:
例子:
SELECT SEC_TO_TIME(2378);
Output is:
输出是:
00:39:38
So in your case:
所以在你的情况下:
SELECT SEC_TO_TIME(test_duration) as `Time` FORM YOUR_TABLE;
回答by Ivo Geersen
Do you store the time in Unixtime (Unix seconds?). If so, use:
您是否将时间存储在 Unixtime(Unix 秒?)中。如果是这样,请使用:
FROM_UNIXTIME(unix_timestamp, '%h:%i:%s')
回答by luiso1979
There is a MYSQL function that transform seconds to the format "HH:MM:SS":
有一个 MYSQL 函数可以将秒转换为“HH:MM:SS”格式:
SEC_TO_TIME(seconds)
Example:
例子:
SELECT SEC_TO_TIME(3661);
will output:
将输出:
01:01:01
01:01:01
Regards
问候
回答by Sergey Dirin
The max value that you can get from the function SEC_TO_TIME is 838:59:59
您可以从函数 SEC_TO_TIME 中获得的最大值是 838:59:59
If you have bigger number you can use this code:
如果您有更大的数字,您可以使用以下代码:
SELECT DAY, Floor((SUM(DAY) / (60*60))) AS Hours,
Floor(MOD(SUM(DAY), (60*60)) / 60) AS Minutes,
Floor(MOD(SUM(DAY), (60))) AS Secs,
CONCAT(
Floor((SUM(DAY) / (60*60))), ":",
Floor(MOD(SUM(DAY), (60*60)) / 60), ":",
Floor(MOD(SUM(DAY), (60)))
) AS Time
FROM (SELECT 5328089 as DAY) a