我正在使用一种方法,检查时间范围是否属于一些空闲时段。
我有一个以unixtime格式array['from','to']
返回空闲插槽的方法。看起来像是:
07:00 - 07:45
07:45 - 08:30
14:30 - 15:15
15:15 - 16:00
我想检查给定范围是否属于这些位置。它们可以重叠插槽,但不应跨越空闲时段。
因此有效范围是:
07:00 - 07:45
07:00 - 08:30
07:10 - 08:20 // also valid
14:30 - 16:00 // also valid
无效范围
06:00 - 16:45
15:15 - 16:10
07:00 - 16:00 //there are no ranges between 8:30 and 14:30
11:00 - 12:00
修改
到目前为止我要检查的是:
$startsinfreeslot=true;
$endrange=0;
foreach ($arr as $slot) {
$from=$slot['from'];
$to=$slot['to'];
if ($from==$start_date && $to==$end_date) {
// given date matches a free slot
return true;
}
if ($start_date>=$from && $start_date<$to) {
$startsinfreeslot=true;
$endrange=$to;
continue;
}
if ($startsinfreeslot) {
// we need to check if range continues
if ($endrange==$from) {
// range stops in the current range
if ($end_date<=$to) {
return true;
} else {
$endrange=$to;
continue;
}
} else {
// range doesn't continued return false
return false;
}
}
}
答案 0 :(得分:1)
假设代码的最后一行是:return false。
您的代码需要检查最后一个空闲插槽的另一个案例。如果$ start_date&gt; = $ from和$ end_date&lt; = $ to($ from和$ to是最后一个广告位),那么您的代码将返回false。所以在循环之后,你应该检查最后一个插槽。