我正在遍历一个对象数组,每个对象都有一个时间属性。当我遍历数组时,我想计算总时间。不确定如何在时间中添加分钟。请参阅此屏幕截图Only hours are getting added。添加时间的代码写在下面
$total_human_readable_time += humanReadableTime($total_workhours);
函数humanReadableTime定义如下
function humanReadableTime($time) {
$time = abs($time);
$hours = floor($time);
$minutes = ceil(($time - floor($time)) * 60);
if ($minutes < 10) {
$minutes = "0$minutes";
}
return $hours . ":" . $minutes;
}
答案 0 :(得分:0)
如果你的humanReadableTime
功能已经按照你想要的那样做了,看起来你应该做的就是在你迭代时不要把它叫做,而只是加起来未格式化的值,然后在结束时调用一次。
foreach($objects as $object) {
// something that gives you $total_workhours
$total_time += $total_workhours;
}
$total_human_readable_time = humanReadableTime($total_time);