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.