title: How to sum time in php?
body: <p>PHP: How to sum the result of a foreach statement?</p>
  
  <p>Below is my working code.  As you will see, I'm trying to count the days in each month and then sum them.</p>
  
  <pre><code>//  A function to calculate days in a given month using just the date
  function daysInMonths($date)
  {
      $month = date("m", strtotime($date));
      $year =  date("Y", strtotime($date)); 
      $num = cal_days_in_month(CAL_GREGORIAN, $month, $year); 
      return $num;
  }       
  
  // A function that places the date of an unknown number of months into an array:
  function getNextMonths($date, $numberOfMonths)
  {
      $timestamp_now = strtotime($date);
      $months[] = date('Y-m-d', $timestamp_now);
  
      for($i = 1;$i &lt;= $numberOfMonths; $i++)
          {
              $months[] = date('Y-m-d', (strtotime($months[0].' +'.$i.' month')));
          }
  
  // counts the days in each month:
      $j=0;
      foreach ($months as $days)
      {
          echo "$j:".daysInMonths($days)."&lt;br&gt;";
          ++$j;            
      }
      print_r($months);
  }
  
  getNextMonths('2011-11-1', '4');
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>Array ( 
      [0] =&gt; 2011-11-01 
      [1] =&gt; 2011-12-01 
      [2] =&gt; 2012-01-01 
      [3] =&gt; 2012-02-01 
      [4] =&gt; 2012-03-01 
  )
  </code></pre>
  
  <p>After counting:<br>
  0:30<br>
  1:31<br>
  2:31<br>
  3:29<br>
  4:31  </p>
  
  <p>This is all correct, I'm just having trouble summing the array after I have the days of the month counted.</p>