title: PHP show array of short months as a range
body: <p>I need to show an array of short month dates as a range. I.e. Where there is an array of consecutive months, output the lowest and highest month and place the text "to" between them.</p>
  
  <p>e.g. array 'Feb, Mar, Apr,  May, Jun, Jul, Aug, Sep, Oct' </p>
  
  <p>output as 'Feb to Oct'</p>
  
  <p>And if there is more than one group of consecutive months, do the above for each group </p>
  
  <p>e.g. array 'Feb, Mar, Apr, May, Aug, Sep, Oct'</p>
  
  <p>output as 'Feb to May, Aug to Oct'</p>
  
  <p>I've got so far with the below code but the output is missing out certain months.  i.e. the below code outputs 'Mar, May, Aug, Oct'</p>
  
  <pre><code>$seasons_raw = 'Feb, Mar, Apr, May, Aug, Sep, Oct';
  $seasons_arr = explode(",",$seasons_raw);
  $seasons_numeric = array();
  $seasons_ranges = array();
  $group_id = 0;
  $last = false;
  foreach($seasons_arr as $season_raw){
      $month = (int)date('m', strtotime($season_raw));
      if($month - $last &gt; 1){
          $group_id++;
      }   
      $seasons_numeric[$group_id][] = $month;
      $last = $month;
  }
  
  foreach($seasons_numeric as $season_section){
      $first_obj   = DateTime::createFromFormat('!m', $season_section[0]);
      $last_obj   = DateTime::createFromFormat('!m', $season_section[count($season_section)-1]);
      if($first_obj == $last_obj){
          $seasons_ranges[] = $first_obj-&gt;format('M');
      }
      else{
          $seasons_ranges[] = $first_obj-&gt;format('M') . ' to ' . $last_obj-&gt;format('M');
      }
  }
  $seasons = implode(', ',$seasons_ranges);
  echo $seasons;
  </code></pre>
  
  <p>Thanks</p>