title: Php DateTime and timestamp calculation
body: <p>I'm trying to get an array of dates to populate a calendar and I'm facing an the issue of infinite loop.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>$month_length = 1;
  $day_begin = 9;
  $day_end = 19;
  $event_interval = 15;
  
  $date = new DateTime();
  
  $today_date_name = $date-&gt;format("D");
  $today_date_number = $date-&gt;format("j");
  $today_date_month = $date-&gt;format("M");
  $today_date_month_number =$date-&gt;format("n");
  $today_date_year = $date-&gt;format("Y");
  
  
  for ($length = 0; $length &lt;= ($month_length - 1); $length++)
  {
      $date-&gt;modify("+$length month"); 
      $current_date_name = $date-&gt;format("D");
      $current_date_number = $date-&gt;format("j");
      $current_date_month = $date-&gt;format("M");
      $current_date_month_number = $date-&gt;format("n");
      $current_date_year = $date-&gt;format("Y");
  
      //calculate the length of the month
      $current_month_length = cal_days_in_month(CAL_GREGORIAN, $current_date_month_number, $current_date_year);
  
      if($current_date_month_number != $today_date_month_number){
          // if we are not in the current month, start the second loop
          // the first of the month.
          $current_date_number = 1;
      }
  
      for($current_date_number; $current_date_number &lt;= $current_month_length; $current_date_number++)
      {
          $date-&gt;setDate($current_date_year, $current_date_month_number, $current_date_number);
  
          //set the ending before because of the loop;
  
          //set the ending of the day
          $date-&gt;setTime($day_end, 0, 0);
          //get the timestamp of beginning
          $ending_timestamp = $date-&gt;format("U");
  
          //set the beginning of the day
          $date-&gt;setTime($day_begin, 0, 0);
          //get the timestamp of beginning
          $beginning_timestamp = $date-&gt;format("U");
  
          $day_length = $ending_timestamp - $beginning_timestamp;
  
          //60 seconds for 1min
          $interval = 60 * $event_interval;
  
          for($the_timestamp = 0; $the_timestamp &lt;= $day_length ; $the_timestamp + $interval)
          {
              $current_timestamp = $beginning_timestamp + $the_timestamp;
              $date-&gt;setTimestamp($current_timestamp);
              $final_array[$current_date_year][$current_date_number . " " . $current_date_month][$current_timestamp] = $date-&gt;format("H : i");
          }
  
      }
  }
  </code></pre>
  
  <p>the last "for" loop ends with an infinite loop and so reaches the maximum execution time, it must be done under 30s.</p>