我需要将秒转换为“小时:分钟:秒”。

例如:“685”转换为“00:11:25”

我怎样才能做到这一点呢?


当前回答

See:

    /** 
     * Convert number of seconds into hours, minutes and seconds 
     * and return an array containing those values 
     * 
     * @param integer $inputSeconds Number of seconds to parse 
     * @return array 
     */ 

    function secondsToTime($inputSeconds) {

        $secondsInAMinute = 60;
        $secondsInAnHour  = 60 * $secondsInAMinute;
        $secondsInADay    = 24 * $secondsInAnHour;

        // extract days
        $days = floor($inputSeconds / $secondsInADay);

        // extract hours
        $hourSeconds = $inputSeconds % $secondsInADay;
        $hours = floor($hourSeconds / $secondsInAnHour);

        // extract minutes
        $minuteSeconds = $hourSeconds % $secondsInAnHour;
        $minutes = floor($minuteSeconds / $secondsInAMinute);

        // extract the remaining seconds
        $remainingSeconds = $minuteSeconds % $secondsInAMinute;
        $seconds = ceil($remainingSeconds);

        // return the final array
        $obj = array(
            'd' => (int) $days,
            'h' => (int) $hours,
            'm' => (int) $minutes,
            's' => (int) $seconds,
        );
        return $obj;
    }

From:将秒转换为日、时、分和秒

其他回答

// 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

写这样的函数返回一个数组

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;
}

然后像这样简单地调用函数:

secondsToTime(100);

输出是

Array ( [h] => 0 [m] => 1 [s] => 40 )

这是一种很好的方法:

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;
    }

解决方案来自:https://gist.github.com/SteveJobzniak/c91a8e2426bac5cb9b0cbc1bdbc45e4b

这段代码尽可能地避免了乏味的函数调用和逐条的字符串构建,以及人们为此而创建的大而笨重的函数。

它返回格式为“1h05m00s”的输出,并使用前导零表示分和秒,只要在它们之前有另一个非零时间组件。

它还跳过所有空的前导组件,以避免给你无用的信息,如“0h00m01s”(相反,它将显示为“1s”)。

示例结果:“1s”、“1m00s”、“19m08s”、“1h00m00s”、“4h08m39s”。

$duration = 1; // values 0 and higher are supported!
$converted = [
    'hours' => floor( $duration / 3600 ),
    'minutes' => floor( ( $duration / 60 ) % 60 ),
    'seconds' => ( $duration % 60 )
];
$result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' );
if( $result == 's' ) { $result = '0s'; }

如果你想让代码更短(但可读性更差),你可以避免$转换数组,而是直接将值放在sprintf()调用中,如下所示:

$duration = 1; // values 0 and higher are supported!
$result = ltrim( sprintf( '%02dh%02dm%02ds', floor( $duration / 3600 ), floor( ( $duration / 60 ) % 60 ), ( $duration % 60 ) ), '0hm' );
if( $result == 's' ) { $result = '0s'; }

在上面的两个代码段中,持续时间必须为0或更高。不支持负持续时间。但是你可以使用下面的代码来处理负持续时间:

$duration = -493; // negative values are supported!
$wasNegative = FALSE;
if( $duration < 0 ) { $wasNegative = TRUE; $duration = abs( $duration ); }
$converted = [
    'hours' => floor( $duration / 3600 ),
    'minutes' => floor( ( $duration / 60 ) % 60 ),
    'seconds' => ( $duration % 60 )
];
$result = ltrim( sprintf( '%02dh%02dm%02ds', $converted['hours'], $converted['minutes'], $converted['seconds'] ), '0hm' );
if( $result == 's' ) { $result = '0s'; }
if( $wasNegative ) { $result = "-{$result}"; }
// $result is now "-8m13s"

如果你需要在javascript中做到这一点,你可以在这里回答的一行代码中做到这一点,用javascript将秒转换为HH-MM-SS。用您想要转换的内容替换SECONDS。

var time = new Date(SECONDS * 1000).toISOString().substr(11, 8);