$times = [
"9:00:00",
"8:00:00",
"9:00:00",
];
// Converting the time to seconds makes calculations
// more simple and easier to understand.
function timeToSeconds($time) {
list($hours, $minutes, $seconds) = explode(":", $time);
return ($hours * 60 * 60) + ($minutes * 60) + $seconds;
}
// Let's use this to convert say 300s into 00:05:00
function formatSecondsAsHMI($seconds) {
return sprintf(
'%02d:%02d:%02d',
floor($seconds/3600),
floor($seconds/60 % 60),
floor($seconds % 60)
);
}
// Add an array of times together and return the formatted string hh:mm:ss
function addTimes($times) {
$seconds = array_sum(array_map(function ($time) {
return timeToSeconds($time);
}, $times));
return formatSecondsAsHMI($seconds);
}
// Subtract an array of times. Order of array important.
// Subtracts 0 from 1 from 2 where 0,1,2 are array keys
// i.e. [03:00:00, 10:00:00] would subtract 3 from 10 = 07:00:00
function subtractTimes($times) {
$times = array_map(function($time) {
return timeToSeconds($time);
}, $times);
return array_reduce($times, function($carry, $item) {
return ($item - $carry);
});
}
// Now just add the times together and subtract the result from 45
echo subtractTimes([addTimes($times), '45:00:00']);
это только когда-либо будет полный день или есть изменение, которое вы могли бы иметь '«8:30:00»'? –