<?php

function get_months($startstring, $endstring)
{
    $time1 = strtotime($startstring); //absolute date comparison needs to be done here, because PHP doesn't do date comparisons
    $time2 = strtotime($endstring);
    $my1   = date('mY', $time1); //need these to compare dates at 'month' granularity
    $my2   = date('mY', $time2);
    $year1 = date('Y', $time1);
    $year2 = date('Y', $time2);
    $years = range($year1, $year2);
    
    foreach ($years as $year) {
        $months[$year] = array();
        while ($time1 < $time2) {
            if (date('Y', $time1) == $year) {
                $months[$year][] = date('m', $time1);
                $time1           = strtotime(date('Y-m-d', $time1) . ' +1 month');
            } else {
                break;
            }
        }
        continue;
    }
    
    return $months;
}

?>



<?php

$montharr = get_months('2003-01-04', '2005-09-18');
foreach (array_keys($montharr) as $year) {
    foreach ($montharr[$year] as $month) {
        print "{$year}-{$month}\n";
    }
}

?>