简体   繁体   English

获取每个日期 PHP 的 start_time 和 end_time 的总小时数?

[英]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:你正在这样做:

  • calculating the diff for the first range: 3:30计算第一个范围的差异:3:30
  • calculating the diff for the second range (3:00) and adding it: 3:30 + 3:00 = 06:30计算第二个范围 (3:00) 的差异并将其添加:3:30 + 3:00 = 06:30
  • because the start of the second range is before the previous end, you are subtracting the diff between them.因为第二个范围的开始在前一个结束之前,所以您要减去它们之间的差异。 the diff between 14:30 and 17:45 is 3:15, so 6:30 - 3:15 = 3:15 14:30 和 17:45 之间的差异是 3:15,所以 6:30 - 3:15 = 3:15
  • but the second range already ends at 17:30, so you are missing the 15 minutes between 17:30 and 17:45.但是第二个范围已经在 17:30 结束,因此您错过了 17:30 到 17:45 之间的 15 分钟。

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.

相关问题 使用 PHP 从具有 start_time 和 end_time 的多维数组中查找总小时数? - Find total hours from multi-dimensional array with start_time and end_time using PHP? sql使用start_time和end_time进行复杂数据排序 - sql Complex data sorting with start_time and end_time PayPal + PHP - 获取收款(开始时间/结束时间) - PayPal + PHP - Fetch incoming payments (start_time / end_time) 我想要`start_time` 和`end_time` 计数如何得到这个? - I want to `start_time` and `end_time` count how can get this? 7个SQL行中的第一个开始时间和最后一个结束时间,仅显示1个SQL结果 - First start_time and last end_time from 7 SQL rows to show only 1 SQL result 如果在范围内找到,如何找到 start_time 和 end_time 范围,然后检查 start_date 和 end_date - How to find the start_time and end_time range if found in the range then check start_date and end_date Laravel 5.1获取start_time和end_time之间的行(两个不同的字段) - Laravel 5.1 Fetch Rows between start_time and end_time (two different fields) 如何在yii2中使用start_time和end_time获取MySql中已有的记录? - How to fetch already existing records in MySql with start_time and end_time in yii2? php通过一天中的多个条目获取从开始时间到结束时间的总工作时间 - php get total worked hours from start time to end time with multiple entries for day 我想显示当前时间不在(column)start_time和end_time(column)之间的所有人员记录 - I want to display all personnel records that the current time is not between to the (column)start_time and end_time(column)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM