title: How to find next available time-slot for asset_type with multiple assets of type
body: <p>I've got a table with reservations for my assets.
  We have n-number of assets <code>$nof</code>, which can be reserved with increments of 15 minutes between an opening hour <code>$open</code> and closing hour <code>$closed</code></p>
  
  <p>If there aren't any assets available for a chosen point in time, I want to find the next available time where there is at least 1 asset available for 60 minutes.</p>
  
  <p>I now iterate with increments of 15 minutes over the table to check for availability, and break out of the loop when <code>availability &gt; 0</code> happened more then 4 times (4*15m) resetting it when <code>availability ==0</code>.</p>
  
  <p>But this just feels not right. And besides it's slow :(</p>
  
  <p>This is an example I distilled from my database. <code>asset_type</code> comes from another table which is linked on the <code>asset_id</code> in an <code>asset</code> table. For clarity i'm ommitting this join, and representing it as column.</p>
  
  <pre><code>Reservations
  id   | asset_id | asset_type | start            | end              |
  -----+----------+------------+------------------+------------------+ 
  2    | 67       | 99         | 2017-01-16 11:00 | 2017-01-16 14:00 |
  4    | 67       | 99         | 2017-01-16 14:30 | 2017-01-16 15:45 |
  3    | 54       | 99         | 2017-01-16 12:30 | 2017-01-16 16:00 |
  </code></pre>
  
  <p>In total there are 2 assets of type 99 normally available: 67 and 54.</p>
  
  <p>So:</p>
  
  <pre><code>// nextAvailable($assesTime, $gap_in_minutes, $nof_assets, $asset_type);
  
  echo nextAvailable('2017-01-16 11:30', 60, 2, 99);
  // 15:45, 
  // not 14:00 as there is only 30 minutes between res#2 and #4
  </code></pre>
  
  <p>What would be the best approach to tackle this problem?</p>