I have the total work hours saved in database as text like this: hhhhh:mm, for example a car has 789:07 in hours work (789 hours and 7 minutes). That work hours can be added or subtracted, what i do i split the string in hours and minutes and made the operation like this:
function addHours($initialTime, $endTime){
$hourStart = $this->getHoursMinutes($initialTime);
$hourEnd = $this->getHoursMinutes($endTime);
$totalMinutes = $hourStart[1] + $hourEnd[1];
$totalHours = 0;
if($totalMinutes >= 60){
$totalHours = 1;
$totalMinutes -= 60;
}
$totalHours = $hourStart[0] + $hourEnd[0];
return sprintf("%02d", $totalHours).":".sprintf("%02d", $totalMinutes);
}
The substract
method is:
function substractHours($initialTime, $endTime){
$hourStart = $this->getHoursMinutes($initialTime);
$hourEnd = $this->getHoursMinutes($endTime);
$totalHours = $hourEnd[0] - $hourStart[0];
$totalMinutes = $hourEnd[1] - $hourStart[1];
if($totalMinutes <0){
$totalMinutes += 60;
$totalHours--;
}
return sprintf("%02d", $totalHours).":".sprintf("%02d", $totalMinutes);
}
The methods works until I had to subtract two hours:
50:00 - 50:06
The result must be -00:06 (like excel function) but i got is -1:54 and i really doesn't know how to resolve this issue.
I though other way with PHP functions or example code in forums and posts but it works with 24:00 as maximum value.