几年,几个月和几周工作的数组,但一年的第01周显示不正确

I have an array of years, months and weeks.

//Returns an array containing the years, months and week numbers between two dates
function year_month($start_date, $end_date)
{
    $begin = new DateTime( $start_date );
    $end = new DateTime( $end_date);
    $end->add(new DateInterval('P1W')); //Add 1 week to include the end date as a week
    $interval = new DateInterval('P1W'); //Add 1 week
    $period = new DatePeriod($begin, $interval, $end);
    $aResult = array();
    foreach ( $period as $dt )
    {
        $aResult[$dt->format('Y')][$dt->format('M')][] = "W".$dt->format('W');
    }

    return $aResult;
}
echo '<pre>';
print_r(year_month("25-11-2013","26-01-2014"));
echo '</pre>';

it outputs the following:

   Array
 (
   [2013] => Array
    (
        [Nov] => Array
            (
                [0] => W48
            )

        [Dec] => Array
            (
                [0] => W49
                [1] => W50
                [2] => W51
                [3] => W52
                [4] => W01
            )

    )

[2014] => Array
    (
        [Jan] => Array
            (
                [0] => W02
                [1] => W03
                [2] => W04
                [3] => W05
            )

    )

)

Notice that w01 is in the Dec array instead of the January array. I assume this is because Monday is the start of each week and in this case Monday is the 30th of December. Any ideas on how to get around this? In fact I am not sure what to do with border cases in general. If a week starts in one month but ends in another it should not be inserted into both months I would rather it be in the month in which it ends. But not sure how to go about this.

Use the thursday in the week of the starting date as the starting point, before adding weeks. The thursday will always be in the correct month (since that's what we use to decide if we're going to have week 53 or week 1 in the last week of December). If that thursday is in January, you're going to get W1 and January, if it's in December, you're going to get W53 and December.

If you want the month the week ends each time, use the sunday as the starting point instead.

You assume right, week "belongs" to month in which week start date is, so your output is correct.

If you wish to set month to last day in week, this can be accomplished with setISODate() method:

foreach ($period as $dt) {
    $dt->setISODate($dt->format('o'), $dt->format('W'), 7);
    $aResult[$dt->format('Y')][$dt->format('M')][] = "W".$dt->format('W');
}

But this will now work for every week that has dates in 2 different months, W05 is now in February.

Demo