Get Month names between given dates in PHP

by satheeshkumar 2014-06-10 15:47:50

To get the month names between given two dates in PHP,
<?php
function get_months($date1, $date2)
{ 
   $time1  = strtotime($date1); 
   $time2  = strtotime($date2); 
   $my     = date('mY', $time2); 
   $months = array(date('Y-m', $time1)); 
   while($time1 < $time2)
   { 
      $time1 = strtotime(date('Y-m', $time1).' +1 month'); 
      if(date('mY', $time1) != $my && ($time1 < $time2)) 
        $months[] = date('Y-m', $time1); 
   }
   $months[] = date('Y-m',$time2);
   return array_unique($months); 
}

$from = "2014-01-01";
$to = "2014-06-01";
$dates = get_months($from,$to);
print_r($dates);

?>
Result : 
Array ( [0] => 2014-01 [1] => 2014-02 [2] => 2014-03 [3] => 2014-04 [4] => 2014-05 [5] => 2014-06 ) 

It prints the months in between the given dates 2014-01-01 and 2014-06-01.
696
like
0
dislike
0
mail
flag

You must LOGIN to add comments