Using help from here I have built a page with a dropdown that displays timeslots between a start and end date and time. The time intervals then loop through every 15 minutes or 30 minutes depending on the setting.
It is day of week based, but the client now wants us to add the feature to block out a certain date/time within a season.
EG:
Season 1 - 1 April 2023 to 1st October 2023 (open from 07:30 to 21:00)
Rule 1 - 1st June 2023 closed between 09:00 and 11:00 Rule 2 - 2nd June 2023 closed between 09:00 and 11:00
With my example code below I have adapted line 67 but it doesnt display the times as closed, can anyone help please?
<?php
$airfield_id = '1';
include "../admin/includes/global.php";
if(!isset($_GET['date'])){
$date = date('d-m-Y');
}else{
$date = $_GET['date'];
}
if(!isset($_GET['date'])){
list($day, $month, $year) = explode('-', date('d-m-Y 00:00:00'));
$timestamp = $month.'/'.$day.'/'.$year;
$timestamp = strtotime($timestamp);
}else{
list($day, $month, $year) = explode('-', $_GET['date']);
$timestamp = $month.'/'.$day.'/'.$year;
$timestamp = strtotime($timestamp);
}
// see what season we are in
$sql = "SELECT * FROM `seasons` WHERE `season_from` <= '".$timestamp."' AND `season_to` >= '".$timestamp."' AND `airfield_id` = '".$airfield_id."' LIMIT 1";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$season_id = $row['season_id'];
$open_from = $row['open_from'];
$open_to = $row['open_to'];
}
$startTime = $open_from;
$endTime = $open_to;
$interval = 15; // minutes
$weekNumber = date('w', strtotime($_GET['date']));
$allowed = 1;
$sql2 = "SELECT * FROM timetable WHERE airfield_id = '".$airfield_id."' AND season = '".$season_id."'";
$result2 = $conn->query($sql2);
$excludedSlots = array();
while($row = $result2->fetch_assoc()) {
foreach(json_decode($row['timetable_days']) as $tday){
$excludedSlots[] = array('day' => $tday, 'start' => $row['start_time'], 'end' => $row['end_time'], 'start_date' => $row['start_date'], 'end_date' => $row['end_date']);
}
}
print_r($excludedSlots);
//$excludedSlots = [
//['day' => '4', 'start' => '10:00', 'end' => '12:00'],
//['day' => '4', 'start' => '12:30', 'end' => '13:30'],
//['day' => '4', 'start' => '15:30', 'end' => '16:00']
//];
$timeRange = generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots, $conn, $airfield_id, $allowed);
echo $timeRange;
function generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots, $conn, $airfield_id, $allowed) {
$timeRange = '';
$currentTime = $startTime;
$currentDate = time();
while ($currentTime <= $endTime) {
$isExcluded = false;
foreach ($excludedSlots as $excludedSlot) {
if ($weekNumber == $excludedSlot['day'] && $currentTime >= $excludedSlot['start'] && $currentTime <= $excludedSlot['end'] && $currentDate >= $excludedSlot['start_date'] && $currentDate <= $excludedSlot['end_date']) {
$isExcluded = true;
break;
}
}
if(!isset($_GET['date'])){
$date = date('d-m-Y');
}else{
$date = $_GET['date'];
}
$timestamp = strtotime($date." ".$currentTime);
$booked = "SELECT * FROM `requests` WHERE `date` = '".$timestamp."' AND `airfield_id` = '".$airfield_id."' AND status != 'denied'";
$result = $conn->query($booked);
$num_reqs = mysqli_num_rows($result);
if (!$isExcluded && !$num_reqs >= $allowed) {
$timeRange .= "<option value='{$currentTime}'>{$currentTime}</option>";
} else {
$timeRange .= "<option value='{$currentTime}' disabled>{$currentTime} (Unavailable)</option>";
}
$currentTime = date('H:i', strtotime($currentTime) + ($interval * 60));
}
return $timeRange;
}
?>
Edited line 67 by adding:
&& $currentDate >= $excludedSlot['start_date'] && $currentDate <= $excludedSlot['end_date']
And adding:
$currentDate = time();