[英]Get total hours from start_time and end_time for each date PHP?
I am using below code to find out correct total hours but it is giving wrong results:- Using getSortedDays to sort my array Using addTotalAttendedHours to add total and add it into total attended hours,I have tried all possible methods of time and none of them are working.我正在使用下面的代码来找出正确的总小时数,但它给出了错误的结果:- 使用 getSortedDays 对我的数组进行排序 使用 addTotalAttendedHours 来添加总数并将其添加到总出席小时数中,我已经尝试了所有可能的时间方法,但没有一个在工作中。 Even I tried removing abs and round but nothing is helping
即使我尝试去除腹肌和圆形但没有任何帮助
<?php
declare(strict_types=1);
$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',
],
],
];
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']);
$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;
}
echo "<pre>";
print_r(addTotalAttendedHours($data));
?>
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 //It should be 03:30:00
)
[2020-07-15] => Array
(
[0] => Array
(
[start_time] => 09:00:00
[end_time] => 14:00:00
)
[1] => Array
(
[start_time] => 13:30:00
[end_time] => 17:00:00
)
[total_attended_hours] => 08:00:00
)
)
Using above code I am getting wrong total_attended_hours
where for date 2020-07-14
the result should be 03:30:00
but it is giving 03:15:00
使用上面的代码我弄错了
total_attended_hours
日期2020-07-14
结果应该是03:30:00
但它给出的是03:15:00
For the first day you have these time ranges:第一天,您有以下时间范围:
14:15 – 17:45 14:15 – 17:45
14:30 – 17:30 14:30 – 17:30
14:30 – 17:30 14:30 – 17:30
And you are doing this:你正在这样做:
You could change the subtracting line to:您可以将减法线更改为:
$sum->sub($currentStart->diff(min($currentEnd, $previousEnd)));
This way you wouldn't subtract too much anymore, because you would subtract only the diff of the overlapping part until the minimum of current and previous end.这样你就不会再减去太多,因为你只会减去重叠部分的差异,直到当前和前一个结束的最小值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.