How to print all the months and years between two dates in PHP

function get_months($date1, $date2) {
$time1 = strtotime($date1);
$time2 = strtotime($date2);
$my = date(‘mY’, $time2);

$months = array(date(‘F Y’, $time1));

while($time1 < $time2) {
$time1 = strtotime(date(‘Y-m-d’, $time1).’ +1 month’);
if(date(‘mY’, $time1) != $my && ($time1 < $time2))
$months[] = date(‘F Y’, $time1);
}

$months[] = date(‘F Y’, $time2);
return $months;
}
echo “<pre>”;
print_r(get_months(’2011-02-1′, ’2012-12-1′));

Leave a Reply

Your email address will not be published. Required fields are marked *


six × 8 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>