- sscanf: documentation ( source)
- floor: documentation ( source)
- sprintf: documentation ( source)
<?php
function strToDuration($str) {
sscanf($str, '%d:%d:%d', $hours, $minutes, $seconds);
return $seconds + ($minutes * 60) + ($hours * 60 * 60);
}
function durationToStr($duration) {
$hours = floor($duration / (60 * 60));
$seconds = $duration % (60 * 60);
$minutes = floor($seconds / 60);
$seconds %= 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
$morning = '54:05:12';
$evening = '123:05:29';
echo durationToStr(strToDuration($morning) + strToDuration($evening));