How to get a list of month between two dates using PHP

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 ) 
affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “How to get a list of month between two dates using PHP”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>