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

我有一个以下场景,我需要仅检查特定日期数组内的重叠并获取总出勤时间。


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',

    ),

  ),

)

如上面的日期示例所示,2020-07-14我们的start_time and end_time:- 总出席时间为should be equals to7 小时 30 分钟``


接下来2020-07-15应该是total_attended_hours=8 hours 0 mins

操场

以下数组的新问题

$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',

    ],

  ],

];

结果 :-


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

        )

[total_attended_hours] => 03:15:00应该在哪里[total_attended_hours] => 03:30:00


湖上湖
浏览 112回答 1
1回答

慕勒3428872

这里你得到了算法:对于每组时间预订,执行以下操作找到最小的start_time将和duration之间添加到start_timeend_timesum查找下一个最小的时间预订start_timeIF&nbsp;current_end_time<previous_end_time跳转到 4 END IFIF&nbsp;start_time<previous_end_time减去sumEND IF的差值在和duration之间添加start_timeend_time跳到4,直到没有匹配的元素为止。快乐编码:)编辑 - 添加更干净的实现function getSortedDays(array $days): array {&nbsp; &nbsp; return array_map(function (array $day) {&nbsp; &nbsp; &nbsp; &nbsp;array_multisort(array_column($day, 'start_time'), SORT_ASC, $day);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return $day;&nbsp; &nbsp; }, $days);}function addTotalAttendedHours(array $days): array {&nbsp; &nbsp; $sortedDays = getSortedDays($days);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $days = array_map(function (array $day) {&nbsp; &nbsp; &nbsp; &nbsp; $sum = (new DateTime())->setTimestamp(0);&nbsp; &nbsp; &nbsp; &nbsp; $previousEnd = null;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($day as $time) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentStart = new DateTimeImmutable($time['start_time']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentEnd = new DateTimeImmutable($time['end_time']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($currentEnd < $previousEnd) continue; // this has been added&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sum->add($currentStart->diff($currentEnd));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($previousEnd !== null && $currentStart < $previousEnd) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sum->sub($currentStart->diff($previousEnd));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $previousEnd = $currentEnd;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $attendedSeconds = $sum->getTimestamp();&nbsp; &nbsp; &nbsp; &nbsp; $day['total_attended_hours'] = sprintf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '%02u:%02u:%02u',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attendedSeconds / 60 / 60,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ($attendedSeconds / 60) % 60,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attendedSeconds % 60&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $day;&nbsp; &nbsp; }, $sortedDays);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $days;}工作示例。
打开App,查看更多内容
随时随地看视频慕课网APP