在" 0h 0min 0s"中转换秒数的功能PHP中的格式

时间:2016-04-29 13:04:00

标签: php function time

  • 我无法在这里找到它,所以我创建了这个问题并分享以帮助其他需要它的人!

3 个答案:

答案 0 :(得分:3)

function timeConversion($totalSeconds) {
        $interval = (new DateTime())->diff((new DateTime())->add(new DateInterval("PT${totalSeconds}S")));
        return $interval->format("%hh %imin %ss");
}

答案 1 :(得分:1)

如果您只需要不到24小时,那么:

$totalSeconds = 12345;

$dt = new DateTime('@' . $totalSeconds);
echo $dt->format('H:i:s');

答案 2 :(得分:0)

我无法在这里找到它,所以我分享帮助其他需要它的人!

function timeConversion($totalSeconds) {

  $minutesInAnHour = 60;
  $secondsInAMinute = 60;
  $secondsInAnHour = 3600;

  $seconds = $totalSeconds%$secondsInAMinute;
  $hours = (int)($totalSeconds/$secondsInAnHour);
  $totalMinutes = $totalSeconds - $seconds;
  $minutes = ($totalMinutes-$hours*$secondsInAnHour)/$minutesInAnHour;

  return $hours . "h " . $minutes . "min " . $seconds . "s";

}