计算多个日期之间的小时和分钟数


Count hour and minutes between multiple dates

是否有函数可以计算多个日期之间的总小时分钟数?

我有很多次这样的:

10:00

18:00 | 15

:00 23:00 | 00:00 00:00 | 08:30 16:30 | 00:00 00:00 | 16:00 19:00 | 00:00 00:00

如您所见,有些可能是空的,现在我需要知道我那周工作的总小时数和分钟数。有人知道解决方案吗?

谢谢!

这是一个示例解决方案,包括解析问题中的输入。它不使用 DateTime 类,而是执行一个超级简单的计算来计算总小时数。

$shifts = "10:00 18:00 | 15:00 23:00 | 00:00 00:00 | 08:30 16:30 | 00:00 00:00 | 16:00 19:00 | 00:00 00:00";
$shifts= explode('|', $shifts);
$sum = 0;    
foreach($shifts as $shift) {
    $times = explode(' ', trim($shift));
    $start = explode(':', $times[0]);
    $end = explode(':', $times[1]);
    $sum += ($end[0] - $start[0]) + ($end[1] - $start[1]) / 100 / 0.6;
}
echo $sum; // prints the result 27

我希望这对你有帮助。

// Create two new DateTime-objects...
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');
// The diff-methods returns a new DateInterval-object...
$diff = $date2->diff($date1);
// Call the format method on the DateInterval-object
echo $diff->format('%d Day and %h hours');