title: Finding the next available timeslot in an array of data
body: <p>I have an array with an 'events' array inside of it that contains an array of events.</p>
  
  <pre><code>$data = array(
  'events'=&gt; array(
      array(
      'title' =&gt; 't1',
      'start_time' =&gt; '12:00AM',
      'end_time' =&gt; '1:00AM',
      ),
          array(
      'title' =&gt; 't1',
      'start_time' =&gt; '1:00PM',
      'end_time' =&gt; '1:30PM',
      ),
          array(
      'title' =&gt; 't1',
      'start_time' =&gt; '1:30PM',
      'end_time' =&gt; '2:00PM',
      ),
          array(
      'title' =&gt; 't1',
      'start_time' =&gt; '3:15PM',
      'end_time' =&gt; '3:45PM',
      ),
          array(
      'title' =&gt; 't1',
      'start_time' =&gt; '3:45PM',
      'end_time' =&gt; '4:45PM',
      )
      )
  );
  </code></pre>
  
  <p>Based on the current time, i'm trying to find the next available time slot of at least 15 minutes. So say its currently 1:15PM, the next available time slot would be 2:00PM (based on the supplied array of data). This seems easy enough but i'm stumped.</p>
  
  <p>The code below is where i'm at. It will return the title if there is an event taking place at the current time but i'm not really sure how to get the next time slot that is not being taken.</p>
  
  <pre><code>$time = time();
  $timePlus15 = time() + 60*15;
  
  foreach ($events-&gt;events as $key =&gt; $event) {
      $start_time = strtotime($event-&gt;start_time);
      $end_time = strtotime($event-&gt;end_time);
  
          if (($time &gt; $start_time &amp;&amp; $timePlus15 &lt; $end_time) || $time &gt; $start_time &amp;&amp; $time &lt; $end_time || ($time &lt; $start_time &amp;&amp; $timePlus15 &gt; $start_time &amp;&amp; $timePlus15 &lt; $end_time)) {
              echo $event-&gt;title;
      }
  }
  </code></pre>
  
  <p>Basically, I want to loop through the array. If at the current time there is an event, increment the time by 15 minutes and check again. If there is an event, increment 15 minutes and check again, and repeat until there is no event for the time, then return that time.</p>
  
  <p><strong><em>the code is in php but i'll accept a javascript/jquery answer as well</em></strong></p>
  
  <p>** THE SOLUTION TO MY PROBLEM IS BELOW **</p>
  
  <pre><code>this.setNextAvailableTime = function() {
      var requiredGap = 15 * 60 * 1000;
      var events = app.rooms.events[app.rooms.current_room].events;
      var prev = events[0];
      var firstGap = null;
      if (events.length === 1) {
          firstGap = prev.end_24time;
      } else {
          for (var i = 1; i &lt; events.length; i += 1) {
  
              var current = events[i];
  
              var current_start = new Date();
              var prev_end = new Date();
              current_start.setHours(events[i].start_24time.substring(0,2),events[i].start_24time.substring(3,5), 0);
              prev_end.setHours(prev.end_24time.substring(0,2),prev.end_24time.substring(3,5), 0);
  
              var diff = current_start.getTime() - prev_end.getTime();
  
              if( diff &gt;= requiredGap) {
                  firstGap = prev.end_24time;
                  break;
              }
  
              prev = current;
          }           
      }
      //do something with the firstGap time
      $('#start_time').val(firstGap);
  };
  </code></pre>