Convert seconds to Hour:Minute:Second

前端 未结 27 2141
说谎
说谎 2020-11-22 07:56

I need to convert seconds to \"Hour:Minute:Second\".

For example: \"685\" converted to \"00:11:25\"

How can I achieve this?

相关标签:
27条回答
  • 2020-11-22 08:26

    This is a pretty way to do that:

    function time_converter($sec_time, $format='h:m:s'){
          $hour = intval($sec_time / 3600) >= 10 ? intval($sec_time / 3600) : '0'.intval($sec_time / 3600);
          $minute = intval(($sec_time % 3600) / 60) >= 10 ? intval(($sec_time % 3600) / 60) : '0'.intval(($sec_time % 3600) / 60);
          $sec = intval(($sec_time % 3600) % 60)  >= 10 ? intval(($sec_time % 3600) % 60) : '0'.intval(($sec_time % 3600) % 60);
    
          $format = str_replace('h', $hour, $format);
          $format = str_replace('m', $minute, $format);
          $format = str_replace('s', $sec, $format);
    
          return $format;
        }
    
    0 讨论(0)
  • 2020-11-22 08:27

    This function my be useful, you could extend it:

    function formatSeconds($seconds) {
    
    if(!is_integer($seconds)) {
        return FALSE;
    }
    
    $fmt = "";
    
    $days = floor($seconds / 86400);
    if($days) {
        $fmt .= $days."D ";
        $seconds %= 86400;
    }
    
    $hours = floor($seconds / 3600);
    if($hours) {
        $fmt .= str_pad($hours, 2, '0', STR_PAD_LEFT).":";
        $seconds %= 3600;
    }
    
    $mins = floor($seconds / 60 );
    if($mins) {
        $fmt .= str_pad($mins, 2, '0', STR_PAD_LEFT).":";
        $seconds %= 60;
    }
    
    $fmt .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
    
    return $fmt;}
    
    0 讨论(0)
  • 2020-11-22 08:29

    If you need to do that in javascript, you can do it in just one line of code as answered here Convert seconds to HH-MM-SS with JavaScript. Replace SECONDS with what you want to convert.

    var time = new Date(SECONDS * 1000).toISOString().substr(11, 8);
    
    0 讨论(0)
  • 2020-11-22 08:30

    Use function gmdate() only if seconds are less than 86400 (1 day) :

    $seconds = 8525;
    echo gmdate('H:i:s', $seconds);
    # 02:22:05
    

    See: gmdate()

    Run the Demo


    Convert seconds to format by 'foot' no limit* :

    $seconds = 8525;
    $H = floor($seconds / 3600);
    $i = ($seconds / 60) % 60;
    $s = $seconds % 60;
    echo sprintf("%02d:%02d:%02d", $H, $i, $s);
    # 02:22:05
    

    See: floor(), sprintf(), arithmetic operators

    Run the Demo


    Example use of DateTime extension:

    $seconds = 8525;
    $zero    = new DateTime("@0");
    $offset  = new DateTime("@$seconds");
    $diff    = $zero->diff($offset);
    echo sprintf("%02d:%02d:%02d", $diff->days * 24 + $diff->h, $diff->i, $diff->s);
    # 02:22:05
    

    See: DateTime::__construct(), DateTime::modify(), clone, sprintf()

    Run the Demo


    MySQL example range of the result is constrained to that of the TIME data type, which is from -838:59:59 to 838:59:59 :

    SELECT SEC_TO_TIME(8525);
    # 02:22:05
    

    See: SEC_TO_TIME

    Run the Demo


    PostgreSQL example:

    SELECT TO_CHAR('8525 second'::interval, 'HH24:MI:SS');
    # 02:22:05
    

    Run the Demo

    0 讨论(0)
  • 2020-11-22 08:30
    // TEST
    // 1 Day 6 Hours 50 Minutes 31 Seconds ~ 111031 seconds
    
    $time = 111031; // time duration in seconds
    
    $days = floor($time / (60 * 60 * 24));
    $time -= $days * (60 * 60 * 24);
    
    $hours = floor($time / (60 * 60));
    $time -= $hours * (60 * 60);
    
    $minutes = floor($time / 60);
    $time -= $minutes * 60;
    
    $seconds = floor($time);
    $time -= $seconds;
    
    echo "{$days}d {$hours}h {$minutes}m {$seconds}s"; // 1d 6h 50m 31s
    
    0 讨论(0)
  • 2020-11-22 08:30
    gmdate("H:i:s", no_of_seconds);
    

    Will not give time in H:i:s format if no_of_seconds is greater than 1 day (seconds in a day).
    It will neglect day value and give only Hour:Min:Seconds

    For example:

    gmdate("H:i:s", 89922); // returns 0:58:42 not (1 Day 0:58:42) or 24:58:42
    
    0 讨论(0)
提交回复
热议问题