Php 从繁忙时间数组中获取可用时间
我有一个php的预定时间数组,如下所示:Php 从繁忙时间数组中获取可用时间,php,laravel,Php,Laravel,我有一个php的预定时间数组,如下所示: 打印($data) 现在,我想找到每天可用时间的详细信息。 为了检查这一点,让我们考虑营业时间是从 07:00 00 < /代码>到代码> 20:00时> /p> 现在,我想通过过滤上述所有预定时间来查找这些小时之间的可用时间,即07:00:00到20:00:00 $available_hour = => Array( [2019-09-26] => Array( [
打印($data)
现在,我想找到每天可用时间的详细信息。
为了检查这一点,让我们考虑营业时间是从<代码> 07:00 00 < /代码>到代码> 20:00时> <代码> /p>
现在,我想通过过滤上述所有预定时间来查找这些小时之间的可用时间,即07:00:00
到20:00:00
$available_hour = => Array(
[2019-09-26] => Array(
[0] => Array(
['start'] => 09:00:00
['end'] => 12:00:00
)
[1] => Array(
['start'] => 14:00:00
['end'] => 16:00:00
)
[2] => Array(
['start'] => 17:00:00
['end'] => 19:00:00
)
)
[2019-09-27] => Array(
[0] => Array(
['start'] => 08:00:00
['end'] => 12:00:00
)
[1] => Array(
['start'] => 14:00:00
['end'] => 16:00:00
)
[2] => Array(
['start'] => 18:00:00
['end'] => 19:00:00
)
)
[2019-09-28] => Array(
[0] => Array(
['start'] => 10:00:00
['end'] => 12:00:00
)
[1] => Array(
['start'] => 13:00:00
['end'] => 16:00:00
)
[2] => Array(
['start'] => 17:00:00
['end'] => 19:00:00
)
)
)
这是基本的想法,我在这里看到了类似的堆栈溢出问题,但是,我想用php而不是js来处理它
我已经尽力表示一个示例数据数组(虽然这是一个完整的数组而不是对象),使用它是可以的。这里是链接。试试这段代码
$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;
}
$res=[];
外汇($x为$value)
{
如果(数组键存在($value['start\u date',$res))
{
end($res[$value['start_date']]);//将内部指针移动到数组的末尾
$key=key($res[$value['start_date']]);
if(空($res[$value['start_date'][$key]['end']))
{
如果($value['start\u time']>='07:00:00'&&$value['start\u time']='07:00:00'&&$value['end\u time']]$$value['end\u time'];
}
}
如果(!empty($res[$value['start_date'][$key]['start']))
{
如果($value['end_time']>='07:00:00'&&$value['end_time']]$value['end_time'];
}
}
}
其他的
{
如果($value['start\u date']>='07:00:00'&&$value['start\u date']]“07:00:00”,'end'=>$value['start\u time'];
结束($res[$value['start_date']]);
$key=key($res[$value['start_date']]);
$res[$value['start\u date'][++$key]=['start'=>$value['end\u time']];
}
}
}
foreach($res作为$key=>$result)
{
foreach($k=>v的结果)
{
如果(!array_key_存在('end',$v))
{
未设置($result[$k]);
}
}
$new[$key]=$result;
}
最后的结果将是
$new
变量。假设数组/对象的结构包含“items”,这里有一个很好的小技巧
工作示例:使用PHP5.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);
[EDIT2]
如果您希望人们或您自己更改开放时间,以便id
1
可以将07:00:00
更改为20:00:00
,我建议您将输出结构更改为忽略start
和end
,只选择第一个和最后一个日期:
使用数组function@Usira你能帮我写代码吗?不需要这个函数,只需在数组中循环并创建新的数组就行了。这不起作用,而且没有提到业务周期意味着开始时间是07:00:00和20:00:00。只需更改第一个预定时段的开始时间(2019-09-26)例如,您可以查看此链接,而不是07:00:00至08:00:00,您的代码将失败。您可以告诉我您在哪里包含了营业时间,即上午7点至晚上8点。请检查此代码,我已将第一个开始时间更新为上午8点,然后为什么它仍然显示9-12点的可用时间,而不是显示上午7点至上午8点的可用时间我想你需要把循环时间限制在早上7点到晚上8点之间,而不是从第一个数组元素开始计时啊,是的,你也需要先排序时间,对吗?我想你对这个问题不是很了解。所有数组都是预定的时间。这意味着2019-09-26一个大厅(假设上午7点开门,晚上8点关门)从上述时间开始预订。表示该大厅已预订7-9、12-14、16-17和19-20。这表示该大厅仍可预订9-12、14-16和17-19。但是,如果该大厅已预订8-9、12-14、16-17和19-20,则您的代码在本例中无法在此检查。当此时间仍然为0时,您的结果中不会显示7-8我有空,因为大厅早上7点开门
<?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);
foreach($openinghours as $date => $times) {
echo "{$date} : {$times[0]["start"]} - " . end($times)["end"] . "\n";
}
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";
}