从一系列繁忙时间 PHP 中获取可用时间

我有一个预定时间的php数组:


print_r($data)


Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [0] => stdClass Object

                (

                    [id] => 1

                    [start_date] => 2019-09-26

                    [start_time] => 07:00:00

                    [end_date] => 2019-09-26

                    [end_time] => 09:00:00

                )


            [1] => stdClass Object

                (

                    [id] => 4

                    [start_date] => 2019-09-26

                    [start_time] => 12:00:00

                    [end_date] => 2019-09-26

                    [end_time] => 14:00:00

                )


            [2] => stdClass Object

                (

                    [id] => 9

                    [start_date] => 2019-09-26

                    [start_time] => 16:00:00

                    [end_date] => 2019-09-26

                    [end_time] => 17:00:00

                )


            [3] => stdClass Object

                (

                    [id] => 13

                    [start_date] => 2019-09-26

                    [start_time] => 19:00:00

                    [end_date] => 2019-09-26

                    [end_time] => 20:00:00

                )


            [4] => stdClass Object

                (

                    [id] => 2

                    [start_date] => 2019-09-27

                    [start_time] => 07:00:00

                    [end_date] => 2019-09-27

                    [end_time] => 08:00:00

                )


            [5] => stdClass Object

                (

                    [id] => 5

                    [start_date] => 2019-09-27

                    [start_time] => 12:00:00

                    [end_date] => 2019-09-27

                    [end_time] => 14:00:00

                )

现在,我想查找每天可用时间的详细信息。要检查这一点,让我们考虑营业时间是从07:00:00到20:00:00


07:00:00现在,我想20:00:00通过过滤所有上述预订时间来找到这些时间之间的可用时间。


PIPIONE
浏览 144回答 2
2回答

慕尼黑的夜晚无繁华

这是一个不错的小技巧,假设数组/对象的结构包含“项目”。工作示例:https : //3v4l.org/Wat8i 适用于 PHP 5.4+。<?php$datetimes = [&nbsp; &nbsp; "items" => [&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "07:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "09:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "12:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "14:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "16:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "17:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 13,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "19:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-26",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "20:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "07:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "08:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "12:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "14:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "16:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "18:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 14,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "19:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-27",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "20:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "07:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "10:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 6,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "12:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "13:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 11,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "16:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "17:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id" => 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "start_time" => "19:00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_date" => "2019-09-28",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "end_time" => "20:00:00"&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ]];$openinghours = [];function openinghours($times) {&nbsp; &nbsp; global $openinghours;&nbsp; &nbsp; $openinghours[$times["start_date"]][] = [&nbsp; &nbsp; &nbsp; &nbsp; "start" => $times["start_time"],&nbsp; &nbsp; &nbsp; &nbsp; "end" => $times["end_time"]&nbsp; &nbsp; ];}array_map("openinghours", $datetimes["items"]);var_dump($openinghours);它基本上是在不修改旧数组的情况下创建一个新数组,也不需要 if 语句。[编辑1]foreach()以及快速显示日期和时间的附加功能: https ://3v4l.org/uhUeSforeach($openinghours as $date => $times) {&nbsp; &nbsp; echo "{$date} : {$times[0]["start"]} - " . end($times)["end"] . "\n";}[编辑2]如果您希望人们或您自己更改营业时间以便id 1不得不这样07:00:00做20:00:00,我建议您将输出结构更改为忽略start,end然后只选择日期的第一个和最后一个:https ://3v4l.org/jMvCPfunction openinghours($times) {&nbsp; &nbsp; global $openinghours;&nbsp; &nbsp; $openinghours[$times["start_date"]][] = $times["start_time"];&nbsp; &nbsp; $openinghours[$times["start_date"]][] = $times["end_time"];&nbsp; &nbsp; sort($openinghours[$times["start_date"]]);}array_map("openinghours", $datetimes["items"]);var_dump($openinghours);foreach($openinghours as $date => $times) {&nbsp; &nbsp; echo "{$date} : {$times[0]} - " . end($times) . "\n";}

泛舟湖上清波郎朗

试试这个代码..$res = [];foreach($x as $value){&nbsp; &nbsp; if(array_key_exists($value['start_date'],$res))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; end($res[$value['start_date']]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// move the internal pointer to the end of the array&nbsp; &nbsp; &nbsp; &nbsp; $key = key($res[$value['start_date']]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(empty($res[$value['start_date']][$key]['end']))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($value['start_time'] >= '07:00:00' && $value['start_time'] <= '20:00:00')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$value['start_date']][$key]['end'] = $value['start_time'];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($value['end_time'] >= '07:00:00' && $value['end_time'] <= '20:00:00')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$value['start_date']][] = ['start' => $value['end_time']];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!empty($res[$value['start_date']][$key]['start']))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($value['end_time'] >= '07:00:00' && $value['end_time'] <= '20:00:00')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $res[$value['start_date']][] = ['start' => $value['end_time']];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if($value['start_date'] >= '07:00:00' && $value['start_date'] <= '20:00:00')&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$res[$value['start_date']][] = ['start' => "07:00:00",'end' => $value['start_time']];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end($res[$value['start_date']]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$key = key($res[$value['start_date']]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$res[$value['start_date']][++$key] = ['start' =>&nbsp; $value['end_time']];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}foreach($res as $key => $result){foreach($result as $k => $v){&nbsp; &nbsp; if(!array_key_exists('end',$v))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; unset($result[$k]);&nbsp; &nbsp; }}$new[$key] = $result;}最终结果将是$new可变的。
打开App,查看更多内容
随时随地看视频慕课网APP