skip to Main Content

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


  1. Try this way:

    $interval = "15 minutes";
    ...
    $current    = time();
    add_time   = strtotime($current.'+'.$interval);
    

    Also can you guve us more info about eventual error throwed. Eventually enable debugging:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    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.

    Login or Signup to reply.
  2. Check this out. I hope I’ve understood your problem right and provided the desired solution.

    <?php
    $startTime     = '09:00';
    $endTime       = '17:00';
    $interval      = 15; // minutes
    $weekNumber    = date('w');
    $excludedSlots = [
        ['day' => '4', 'start' => '10:00', 'end' => '12:00'],
        ['day' => '4', 'start' => '12:30', 'end' => '13:30'],
        ['day' => '5', 'start' => '15:30', 'end' => '16:00']
    ];
    
    $timeRange = generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots);
    echo $timeRange;
    
    function generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots) {
        $timeRange = '';
        $currentTime = $startTime;
    
        while ($currentTime <= $endTime) {
            $isExcluded = false;
    
            foreach ($excludedSlots as $excludedSlot) {
                if ($weekNumber == $excludedSlot['day'] && $currentTime >= $excludedSlot['start'] && $currentTime < $excludedSlot['end']) {
                    $isExcluded = true;
                    break;
                }
            }
    
            if (!$isExcluded) {
                $timeRange .= "<option value='{$currentTime}'>{$currentTime}</option>";
            } else {
                $timeRange .= "<option value='{$currentTime}' disabled>{$currentTime} (Blocked)</option>";
            }
    
            $currentTime = date('H:i', strtotime($currentTime) + ($interval * 60));
        }
    
        return $timeRange;
    }
    ?>
    
    
    Login or Signup to reply.
  3. Here is the updated code tested on local machine

    <?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'];          
    } 
    
    
    $times = create_time_range($open_from, $open_to, $interval);
    
    foreach ($times as $key => $time) { 
        $day = date('w', strtotime($date));
        $timeFrame = date('H:i', $time);
       
        foreach ($intervals as $slot) {
            if ($day == $slot['day'] && $timeFrame >= $slot['start'] && $timeFrame < $slot['end']) {
                echo 'No </br>';
            } else {
                echo 'Yes </br>';
            }
        }
    }
    
    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; 
      
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search