0
  • I couldn't find it here, so I'm creating this question and sharing to help someone else that needs it!
William
  • 1,010
  • 3
  • 21
  • 39

3 Answers3

3
function timeConversion($totalSeconds) {
        $interval = (new DateTime())->diff((new DateTime())->add(new DateInterval("PT${totalSeconds}S")));
        return $interval->format("%hh %imin %ss");
}
Chris
  • 5,571
  • 2
  • 20
  • 32
1

If you only need less than 24 hours, then:

$totalSeconds = 12345;

$dt = new DateTime('@' . $totalSeconds);
echo $dt->format('H:i:s');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

I couldn't find it here, so I'm sharing to help someone else that needs it!

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

}
William
  • 1,010
  • 3
  • 21
  • 39