If you need an array representation of a calender month this function will come in handy. This function
will return an array of a given month with days properly distributed in to weeks, as in real calendar.
public function buildMonthCalendar($year, $month) {
$calendar = array(
'week-1' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
'week-2' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
'week-3' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
'week-4' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
'week-5' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
'week-6' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
);
$startOfMonth = "{$year}-{$month}-01";
$result = strtotime("{$year}-{$month}-01");
$endOfMonth = date('Y-m-d', strtotime( date('Y-m-d', strtotime( $result . '+1 month')).' -1 second'));
$j = 1;
for ($i = $startOfMonth; $i != $endOfMonth;) {
$weekDay = date('D', strtotime($i));
$calendar["week-{$j}"][$weekDay] = date('d', strtotime($i));
if ($weekDay == 'Sun') {
$j++; //jump to next week
}
$i = date('Y-m-d', strtotime($i . ' +1 day'));
}
$weekDay = date('D', strtotime($endOfMonth)); //last day of the month
$calendar["week-{$j}"][$weekDay] = date('d', strtotime($endOfMonth));
return $calendar;
}