[英]How to get total hours from start time and end time with multi dimensional array along with over lapping check?
I have a below scenario where I need to check for the overlapping with-in a specific date array only and get total attended hours.我有一个下面的场景,我只需要检查特定日期数组中的重叠并获得总参加时间。
array (
'2020-07-14' =>
array (
'total_attended_hours' => 0,
0 =>
array (
'start_time' => '09:00:00',
'end_time' => '13:00:00',
'hours' => '4 hours 0 mins',
),
1 =>
array (
'start_time' => '13:30:00',
'end_time' => '16:30:00',
'hours' => '3 hours 0 mins',
),
2 =>
array (
'start_time' => '09:00:00',
'end_time' => '14:00:00',
'hours' => '5 hours 0 mins',
),
),
'2020-07-15' =>
array (
'total_attended_hours' => 0,
0 =>
array (
'start_time' => '13:30:00',
'end_time' => '17:00:00',
'hours' => '3 hours 30 mins',
),
1 =>
array (
'start_time' => '09:00:00',
'end_time' => '14:00:00',
'hours' => '5 hours 0 mins',
),
),
)
As in the above example with date 2020-07-14
we have start_time and
end_time :-
total_attended_hours should be equals to
7 hours 30 mins``如上面日期2020-07-14
的示例,我们有start_time and
end_time :-
total_attended_hours should be equals to
7 hours 30 mins``
And for next 2020-07-15
it should be total_attended_hours
= 8 hours 0 mins
对于下一个2020-07-15
,它应该是total_attended_hours
= 8 hours 0 mins
New issue with below array以下数组的新问题
$data = [
'2020-07-14' =>
[
[
'start_time' => '14:15:00',
'end_time' => '17:45:00',
],[
'start_time' => '14:30:00',
'end_time' => '17:30:00',
],[
'start_time' => '14:30:00',
'end_time' => '17:30:00',
],
],
'2020-07-15' => [
[
'start_time' => '13:30:00',
'end_time' => '17:00:00',
],[
'start_time' => '09:00:00',
'end_time' => '14:00:00',
],
],
];
Result:-结果:-
Array
(
[2020-07-14] => Array
(
[0] => Array
(
[start_time] => 14:15:00
[end_time] => 17:45:00
)
[1] => Array
(
[start_time] => 14:30:00
[end_time] => 17:30:00
)
[2] => Array
(
[start_time] => 14:30:00
[end_time] => 17:30:00
)
[total_attended_hours] => 03:15:00
)
Where as [total_attended_hours] => 03:15:00
should be [total_attended_hours] => 03:30:00
其中[total_attended_hours] => 03:15:00
应该是[total_attended_hours] => 03:30:00
Here you got the algorythm:在这里你得到了算法:
start_time
找到最小的start_time
duration
between start_time
and end_time
to a sum
将start_time
和end_time
之间的duration
加到sum
中start_time
按start_time
查找下一个最小的 Time-Bookingcurrent_end_time
< previous_end_time
jump to 4 END IF IF current_end_time
< previous_end_time
跳转到 4 END IFstart_time
< previous_end_time
subtract difference from sum
END IF IF start_time
< previous_end_time
从sum
中减去差 END IFduration
between start_time
and end_time
在start_time
和end_time
之间添加duration
Happy coding:)快乐编码:)
function getSortedDays(array $days): array {
return array_map(function (array $day) {
array_multisort(array_column($day, 'start_time'), SORT_ASC, $day);
return $day;
}, $days);
}
function addTotalAttendedHours(array $days): array {
$sortedDays = getSortedDays($days);
$days = array_map(function (array $day) {
$sum = (new DateTime())->setTimestamp(0);
$previousEnd = null;
foreach ($day as $time) {
$currentStart = new DateTimeImmutable($time['start_time']);
$currentEnd = new DateTimeImmutable($time['end_time']);
if ($currentEnd < $previousEnd) continue; // this has been added
$sum->add($currentStart->diff($currentEnd));
if ($previousEnd !== null && $currentStart < $previousEnd) {
$sum->sub($currentStart->diff($previousEnd));
}
$previousEnd = $currentEnd;
}
$attendedSeconds = $sum->getTimestamp();
$day['total_attended_hours'] = sprintf(
'%02u:%02u:%02u',
$attendedSeconds / 60 / 60,
($attendedSeconds / 60) % 60,
$attendedSeconds % 60
);
return $day;
}, $sortedDays);
return $days;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.