function getDates($year){
$dates = array();
for($i = 1; $i <= 366; $i++){
$month = date('m', mktime(0,0,0,1,$i,$year)); // outputs month 01 for jan etc
$wk = date('W', mktime(0,0,0,1,$i,$year)); // this outputs 01 if 1st week etc
$wkDay = date('D', mktime(0,0,0,1,$i,$year)); //weekday eg mon ,sun etc
echo $day = date('d', mktime(0,0,0,1,$i,$year)); // outputs day eg 01,13,23 etc
$dates[$month][$wk][$wkDay] = $day; // storing date in array
}
return $dates;
}
$dates = getDates(2014);
echo '<br/>'.$dates['01']['01']['Wed']; // getting 01
echo '<br/>'.$dates['01']['01']['Thu']; // 01 (should get 02 as it is 2nd jan thu in 2014)
echo '<br/>'.$dates['01']['01']['Fri']; // 03
when i echo $day in for loop i m receiving 01 02 03 04 etc which is correct but when i echo same in $dates array above i am receiving 01 for both 1st month 1st week wednesday and 1st month 1st week thursday. Why? Where Am i wrong? All other dates i am receiving for 2014 calender is correct.
@ymas is right. Because you are using 366 days in 2014, number 366 is the first day in 2015. This results that the first day of 2015 overwrite the value in your current array. Fix this by check the number of days in the year and use that value instead of the static 366. That will be something like this:
Note that you should not use 365 as static value because then 31 dec will not be available on leap year.