-1

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();
  • Is this a PHP problem, or a MySQL problem? What have you tried to resolve it? Also, please be warned that your queries are widely open for SQL injection - have a look at prepared statements to avoid getting hacked – Nico Haase Aug 11 '23 at 10:45
  • Thanks Nico, this is a PHP issue, this is just test code it isnt in production. I have added this to the excludedSlots array: , 'start_date' => $row['start_date'], 'end_date' => $row['end_date'] and this to the foreach loop && $currentDate >= $excludedSlot['start_date'] && $currentDate <= $excludedSlot['end_date'] But it doesnt work with specific dates – simonfewkes83 Aug 11 '23 at 10:49
  • Please add all clarification to your question by editing it. Also, share what "doesnt work" means - as this is so little code, it should not be too hard to debug it with vardump or XDebug – Nico Haase Aug 11 '23 at 11:22
  • 'Doesnt work' means the dropdown of times (06:00,06:30,.....) does not show the times that are blocked out for that specfic date, even though I have tried to make it date aware. The usual day of the week and time exclusions is working fine but i need to improve it by allowing the time exclusions to work between a date range, eg: 1st Jun-3rd June – simonfewkes83 Aug 11 '23 at 11:30
  • **Please add all clarification to your question by editing it**, along with your attempts to resolve the problem – Nico Haase Aug 11 '23 at 11:36
  • I have done that above, you can see what code I have added to line 67 and the $currentDate variable. I havent done any other code changes. – simonfewkes83 Aug 11 '23 at 11:43
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 11 '23 at 18:03

0 Answers0