- I couldn't find it here, so I'm creating this question and sharing to help someone else that needs it!
Asked
Active
Viewed 62 times
3 Answers
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
-
Oh well, living and learning that there is always a fastest way. Thanks mate! – William Apr 29 '16 at 13:16
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
-
Thanks for sharing this! But I would rather have the possibility to have days also. – William Apr 29 '16 at 13:49
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