I am trying to write an application for booking a time slot at an airfield, basically there is an open and close period and time interval, the application then loops between the two time values and displays a select dropdown.
I need to build in the option for the airfield to block out time ranges, so for example every Thursday between 10:00am and 12:00pm the airfield is closed to bookings, I need to build this into the loop but cannot get it working. It needs to be day specific (Mon-Sun) rather than date specific as the airfield will be closed every specified day each week for the same time period.
Here is my code so far, can anyone help please?
<?php
$open_from = "09:00";
$open_to = "17:00";
$interval = "15 mins";
$today_number = date('w');
$intervals = array(
array(
'day' => '4',
'start' => '10:00',
'end' => '12:00',
),
array(
'day' => '4',
'start' => '12:30',
'end' => '13:30',
),
array(
'day' => '5',
'start' => '15:30',
'end' => '16:00',
),
);
if(!isset($_GET['date'])){
$date = date('d-m-Y');
}else{
$date = $_GET['date'];
}
function create_time_range($start, $end, $interval) {
$start_time = strtotime($start);
$end_time = strtotime($end);
$current = time();
$add_time = strtotime('+'.$interval, $current);
$diff = $add_time-$current;
$times = array();
while ($start_time < $end_time) {
$times[] = $start_time;
$start_time += $diff;
}
$times[] = $start_time;
return $times;
}
$times = create_time_range($open_from, $open_to, $interval);
foreach ($times as $key => $time) {
$times[$key] = date($date.'H:i', $time);
foreach($intervals as $key2 => $item) {
if($intervals[$key2]['day']==$today_number && date('H:i',$time) == $intervals[$key2]['start'] && date('H:i',$time) == $intervals[$key2]['end']){
echo "yes";
}else{
echo "no";
}
echo "<option value='".date('H:i', $timestamp)."'>".date('H:i',$time)."</option><br>";
//echo $intervals[$key]['day']." - ".$intervals[$key]['start']." - ".$intervals[$key]['end'];
//echo "<br>";
}
}
?>
3
Answers
Try this way:
Also can you guve us more info about eventual error throwed. Eventually enable debugging:
UPDATE 1
Ok inside the foreach you try to modify the same array you are looping ($times[$key]) and doesn’t seems to have sense.
Check this out. I hope I’ve understood your problem right and provided the desired solution.
Here is the updated code tested on local machine