title: php get time intervals between 2 times
body: <p>I am trying to create a booking form where a user can select a booking time between 2 given times in 5 minute intervals. For example I want time slots between 10am and 12pm which would give me about 20 time slots.</p>
  
  <p>When the user goes to select a slot, the earliest slot should be at least 15 mins ahead of the current time but the user can select a slot and hour or more if desired.</p>
  
  <p>I found some code on SO (can't remember where) and I've edited it for my needs and it works if the current time is within the start and end time but if the current time is an hour before the earliest time, it doesn't create the time slots.</p>
  
  <p>I know why it does it but i don't know how to fix it. It has to do with the <code>while</code> condition.</p>
  
  <p>I would like to be able to book a slot hours before the first available slot if that is possible.</p>
  
  <pre><code>    $timenow = time();
  
      $start_time = strtotime('+15 minutes', $timenow);
      // round to next 15 minutes (15 * 60 seconds)
      $start_time = ceil($start_time / (5 * 60)) * (5 * 60);
      //set the start times
      $opentime = strtotime('10:00');
      $closetime = strtotime('11:55');
  
      // get a list of prebooked slots from database
      $time_slots = $this-&gt;countStartTimes();
  
      $available_slots = array();
  
      while($start_time &lt;= $closetime &amp;&amp; $start_time &gt;= $opentime) {
  
          $key = date('H:i', $start_time);
  
          if(array_key_exists($key, $time_slots)) {
              if($time_slots[$key] == SLOTS) {
                  $available_slots[] = 'FULL'; 
                  break;
              }
          }
  
          $available_slots[] = date('H:i', $start_time);
  
          $start_time = strtotime('+5 minutes', $start_time);
      }
  </code></pre>