Convert seconds to Hour:Minute:Second

前端 未结 27 2139
说谎
说谎 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:44

    Try this:

    date("H:i:s",-57600 + 685);
    

    Taken from
    http://bytes.com/topic/php/answers/3917-seconds-converted-hh-mm-ss

    0 讨论(0)
  • The following codes can display total hours plus minutes and seconds accurately

    $duration_in_seconds = 86401;
    if($duration_in_seconds>0)
    {
        echo floor($duration_in_seconds/3600).gmdate(":i:s", $duration_in_seconds%3600);
    }
    else
    {
        echo "00:00:00";
    }
    
    0 讨论(0)
  • 2020-11-22 08:46

    write function like this to return an array

    function secondsToTime($seconds) {
    
      // extract hours
      $hours = floor($seconds / (60 * 60));
    
      // extract minutes
      $divisor_for_minutes = $seconds % (60 * 60);
      $minutes = floor($divisor_for_minutes / 60);
    
      // extract the remaining seconds
      $divisor_for_seconds = $divisor_for_minutes % 60;
      $seconds = ceil($divisor_for_seconds);
    
      // return the final array
      $obj = array(
          "h" => (int) $hours,
          "m" => (int) $minutes,
          "s" => (int) $seconds,
       );
    
      return $obj;
    }
    

    then simply call the function like this:

    secondsToTime(100);
    

    output is

    Array ( [h] => 0 [m] => 1 [s] => 40 )
    
    0 讨论(0)
提交回复
热议问题