title: PHP Time loop between start time and end time
body: <p>I have this function that gives me a set of options in a select input.
  The options give me times with 5 minute interval.
  The problem is when the time is like 23:45, the options will start from 00:10 and loops based on the $j variable.</p>
  
  <p>This is what I want to do in words:
  Give me a list of options in 5 minutes interval from $open_time till $close_time.
  If the current Time ($timeNow) is greater than the $open_time, set the $open_time to the $timeNow to be shown as first option.
  Do this loop only until the $close_time.</p>
  
  <p>I hope this is clear.
  Appreciate your help :)</p>
  
  <p>Here is the code:</p>
  
  <pre><code>function selectTimesofDay(){
      $output = "";
      $now = date('G:i ', time()); // time now
      $timeNow = strtotime($now); // strtotime now
      $next_five = ceil($timeNow / 300) * 300; // get next 5 minute
      // time now rounded to next 10 minute
      $round5minNow = date('G:i', strtotime('+15 minutes',$next_five)); 
      $open_time = strtotime('17:00');
      $close_time = strtotime('23:59');
  
      // in the middle of working hours, time sets to current
      if($timeNow &gt;= $open_time &amp;&amp; $timeNow &lt; $close_time){
          $open_time = strtotime($round5minNow);
       }
      $time_diff = round(($close_time - $open_time)/60) ; 
      if(date('l') == 'Friday'){
          $j = ($time_diff/5)+11; // working hours extended untill 1:00 AM
      } else{
          $j = ($time_diff/5)-1; // working hours untill 12:00 AM
      }
  
          for($i = 0; $i &lt;= $j; $i++){
              $b = $i*5;  
              $data = date('l')." - ".date("H:i", strtotime('+'.$b.' minutes', $open_time));
              $output .= "&lt;option value=\"{$data}\"&gt;";    
              $output .= $data;
              $output .= "&lt;/option&gt;";
          }
  
      return $output;
  }
  </code></pre>