为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
PIPIONE 2022-01-24 13:16:00
我有一个预定时间的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:0007:00:00现在,我想20:00:00通过过滤所有上述预订时间来找到这些时间之间的可用时间。
查看完整描述

2 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

这是一个不错的小技巧,假设数组/对象的结构包含“项目”。


工作示例:https : //3v4l.org/Wat8i 适用于 PHP 5.4+。


<?php


$datetimes = [

    "items" => [

        [

            "id" => 1,

            "start_date" => "2019-09-26",

            "start_time" => "07:00:00",

            "end_date" => "2019-09-26",

            "end_time" => "09:00:00"

        ],

        [

            "id" => 4,

            "start_date" => "2019-09-26",

            "start_time" => "12:00:00",

            "end_date" => "2019-09-26",

            "end_time" => "14:00:00"

        ],

        [

            "id" => 9,

            "start_date" => "2019-09-26",

            "start_time" => "16:00:00",

            "end_date" => "2019-09-26",

            "end_time" => "17:00:00"

        ],

        [

            "id" => 13,

            "start_date" => "2019-09-26",

            "start_time" => "19:00:00",

            "end_date" => "2019-09-26",

            "end_time" => "20:00:00"

        ],

        [

            "id" => 2,

            "start_date" => "2019-09-27",

            "start_time" => "07:00:00",

            "end_date" => "2019-09-27",

            "end_time" => "08:00:00"

        ],

        [

            "id" => 5,

            "start_date" => "2019-09-27",

            "start_time" => "12:00:00",

            "end_date" => "2019-09-27",

            "end_time" => "14:00:00"

        ],

        [

            "id" => 10,

            "start_date" => "2019-09-27",

            "start_time" => "16:00:00",

            "end_date" => "2019-09-27",

            "end_time" => "18:00:00"

        ],

        [

            "id" => 14,

            "start_date" => "2019-09-27",

            "start_time" => "19:00:00",

            "end_date" => "2019-09-27",

            "end_time" => "20:00:00"

        ],

        [

            "id" => 3,

            "start_date" => "2019-09-28",

            "start_time" => "07:00:00",

            "end_date" => "2019-09-28",

            "end_time" => "10:00:00"

        ],

        [

            "id" => 6,

            "start_date" => "2019-09-28",

            "start_time" => "12:00:00",

            "end_date" => "2019-09-28",

            "end_time" => "13:00:00"

        ],

        [

            "id" => 11,

            "start_date" => "2019-09-28",

            "start_time" => "16:00:00",

            "end_date" => "2019-09-28",

            "end_time" => "17:00:00"

        ],

        [

            "id" => 15,

            "start_date" => "2019-09-28",

            "start_time" => "19:00:00",

            "end_date" => "2019-09-28",

            "end_time" => "20:00:00"

        ]

    ]

];


$openinghours = [];


function openinghours($times) {


    global $openinghours;


    $openinghours[$times["start_date"]][] = [

        "start" => $times["start_time"],

        "end" => $times["end_time"]

    ];


}


array_map("openinghours", $datetimes["items"]);


var_dump($openinghours);

它基本上是在不修改旧数组的情况下创建一个新数组,也不需要 if 语句。


[编辑1]


foreach()以及快速显示日期和时间的附加功能: https ://3v4l.org/uhUeS


foreach($openinghours as $date => $times) {

    echo "{$date} : {$times[0]["start"]} - " . end($times)["end"] . "\n";

}

[编辑2]


如果您希望人们或您自己更改营业时间以便id 1不得不这样07:00:00做20:00:00,我建议您将输出结构更改为忽略start,end然后只选择日期的第一个和最后一个:https ://3v4l.org/jMvCP


function openinghours($times) {


    global $openinghours;


    $openinghours[$times["start_date"]][] = $times["start_time"];

    $openinghours[$times["start_date"]][] = $times["end_time"];


    sort($openinghours[$times["start_date"]]);


}


array_map("openinghours", $datetimes["items"]);


var_dump($openinghours);


foreach($openinghours as $date => $times) {

    echo "{$date} : {$times[0]} - " . end($times) . "\n";

}


查看完整回答
反对 回复 2022-01-24
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

试试这个代码..


$res = [];

foreach($x as $value)

{

    if(array_key_exists($value['start_date'],$res))

    {

        end($res[$value['start_date']]);         // move the internal pointer to the end of the array

        $key = key($res[$value['start_date']]);

            if(empty($res[$value['start_date']][$key]['end']))

            {

                if($value['start_time'] >= '07:00:00' && $value['start_time'] <= '20:00:00')

                {

                    $res[$value['start_date']][$key]['end'] = $value['start_time']; 

                }

            }

            else

            {

                if($value['end_time'] >= '07:00:00' && $value['end_time'] <= '20:00:00')

                {

                    $res[$value['start_date']][] = ['start' => $value['end_time']];

                }

            }

            if(!empty($res[$value['start_date']][$key]['start']))

            {

                if($value['end_time'] >= '07:00:00' && $value['end_time'] <= '20:00:00')

                {

                    $res[$value['start_date']][] = ['start' => $value['end_time']]; 

                }

            }

    }


    else

    {

        if($value['start_date'] >= '07:00:00' && $value['start_date'] <= '20:00:00')

        {

             $res[$value['start_date']][] = ['start' => "07:00:00",'end' => $value['start_time']];

             end($res[$value['start_date']]);         

             $key = key($res[$value['start_date']]);

             $res[$value['start_date']][++$key] = ['start' =>  $value['end_time']];


        }

    }

}

foreach($res as $key => $result)

{

foreach($result as $k => $v)

{

    if(!array_key_exists('end',$v))

    {

        unset($result[$k]);

    }

}

$new[$key] = $result;

}

最终结果将是$new可变的。


查看完整回答
反对 回复 2022-01-24
  • 2 回答
  • 0 关注
  • 143 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信