2 回答
TA贡献1828条经验 获得超13个赞
您首先需要获得保留的插槽。然后在循环中检查该槽是否有下一个保留槽。
public function getTimeSlots($app)
{
$reservedSlots = Timeslot::where('app', '=', $app)->where('reserved', '=', 1)->orderBy('intervals', 'ASC')->get();
$timeslots = Timeslot::where('app', '=', $app)->where('reserved', '=', 0)->orderBy('intervals', 'ASC')->get();
$timeslotlist = array();
foreach ($timeslots as $timeSlot) {
$haveNextReservedSlot = array_filter($reservedSlots, function($v) use($timeSlot){
$start_date = new DateTime($timeSlot['date'].' '.$timeSlot['intervals']);
$since_start = $start_date->diff(new DateTime($v['date'].' '.$v['intervals']));
return $since_start->i < 15; //if difference less than 15 min
});
if($haveNextReservedSlot) continue;
$timeslotlist[] = array(
'id' => $timeSlot['id'],
'app' => $timeSlot['app'],
'intervals' => $timeSlot['intervals'],
'reserved' => $timeSlot['reserved'],
);
}
return json_encode($timeslotlist);
}
TA贡献1815条经验 获得超13个赞
我想将集合转换为数组,然后比较上一个时隙和下一个时隙会更容易,例如
public function getTimeSlots($app)
{
$timeslots = Timeslot::where('app', '=', $app)->orderBy('intervals', 'ASC')->get()->toArray();
$timeslotlist = array();
for($i=0;$i<count($timeslots);$i++){
$current_slot=strtotime($timeslots[$i]->date." ".$timeslots[$i]->intervals);
if(date('i',$current_slot) % 15===0){
if((isset($timeslots[$i-1]) && $timeslots[$i-1]->reserved) or
(isset($timeslots[$i+1]) && $timeslots[$i+1]->reserved)){
}
else{
$timeslotlist[]=$timeslots[$i];
}
}
}
return json_encode($timeslotlist);
}
这将返回 08:30。这是你想要的吗?
- 2 回答
- 0 关注
- 159 浏览
添加回答
举报