简体   繁体   English

如何使用多维数组以及重叠检查从开始时间和结束时间获取总小时数?

[英]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

Playground操场

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:在这里你得到了算法:

  1. For each set of Time-Bookings do the following对于每组时间预订,请执行以下操作
  2. Find the smallest start_time找到最小的start_time
  3. Add the duration between start_time and end_time to a sumstart_timeend_time之间的duration加到sum
  4. Find the next smallest Time-Booking by start_timestart_time查找下一个最小的 Time-Booking
  5. IF current_end_time < previous_end_time jump to 4 END IF IF current_end_time < previous_end_time跳转到 4 END IF
  6. IF start_time < previous_end_time subtract difference from sum END IF IF start_time < previous_end_timesum中减去差 END IF
  7. Add duration between start_time and end_timestart_timeend_time之间添加duration
  8. Jump to 4 until there is no matching element left.跳到 4 直到没有匹配的元素。

Happy coding:)快乐编码:)

EDIT - add more clean implementation编辑 - 添加更干净的实现

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;
}

Working example .工作示例

声明:本站的技术帖子网页,遵循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? php通过一天中的多个条目获取从开始时间到结束时间的总工作时间 - php get total worked hours from start time to end time with multiple entries for day 获取每个日期 PHP 的 start_time 和 end_time 的总小时数? - Get total hours from start_time and end_time for each date PHP? 如何使用php中的12小时格式获取从开始时间到结束时间的总小时数 - How to get the total hr from start time to end time using 12 Hour Format in php 如何在PHP中获取从开始时间到结束时间的总小时数 - How to get the total hour from starting time to end time in php 如果总时间大于 24 小时,则从 php 中的数组计算总时间 - Calculate Total time from array in php if total time greater than 24 hours 如何从多维数组获取数据 - How to get data from multi dimensional array 如何使用JavaScript检查结束时间和开始时间是否相等 - How to check if end time and start time are equal with javascript 如何使用php和javascript实现从开始到结束日期的总经过时间 - How to implement total elapsed time from start to end date using php and javascript 如何将具有相同结束时间的数组与下一个数组开始时间合并 - How to merge array with same end time with next array start time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM