How to convert time in seconds to HH:MM:SS format in MySQL?

前端 未结 4 1816
太阳男子
太阳男子 2020-12-10 12:40

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

相关标签:
4条回答
  • 2020-12-10 13:23

    There is a MYSQL function that transform seconds to the format "HH:MM:SS":

    SEC_TO_TIME(seconds)
    

    Example:

    SELECT SEC_TO_TIME(3661);
    

    will output:

    01:01:01

    Regards

    0 讨论(0)
  • 2020-12-10 13:25

    Do you store the time in Unixtime (Unix seconds?). If so, use:

    FROM_UNIXTIME(unix_timestamp, '%h:%i:%s')
    
    0 讨论(0)
  • 2020-12-10 13:38

    You can use MySQL function 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;
    
    0 讨论(0)
  • 2020-12-10 13:38

    The max value that you can get from the function SEC_TO_TIME is 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
    
    0 讨论(0)
提交回复
热议问题