Convert seconds to Hour:Minute:Second

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

提交回复
热议问题