This demo will show you how to get a list of month between 2 dates. First, let’s find out how to get the next month :
function get_next_month($tstamp) {
return (strtotime('+1 months', strtotime(date('Y-m-01', $tstamp))));
}
Now, we can get a month list between 2 dates.
print_r(get_month_between_two_date('2012-09-15', '2013-01-15'));
function get_month_between_two_datetime($start, $end){
$start = $start=='' ? time() : strtotime($start);
$end = $end=='' ? time() : strtotime($end);
$months = array();
for ($i = $start; $i <= $end; $i = get_next_month($i)) {
$months[] = date('Ym', $i);
}
return $months;
}
The result will return :
Array ( [0] => 201209 [1] => 201210 [2] => 201211 [3] => 201212 [4] => 201301 )