6 || $dao_adj < 0){ throw new Exception("Invalid starting day of the week parameter"); } if ($date instanceof DateTime) { $d = $date; } else { $d = new DateTime($date); } $dm = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // all days of the month $vals = explode(" ", $d->format('w j L n Y')); // 0 - day of the week // 1 - day of the month // 2 - leap year // 3 - month of year // 4 - year $dm[1] += $vals[2]; // leap year adjustment for February $day_of_week = ($vals[0] + $dao_adj*6) % 7; $first_day = ($day_of_week + ($vals[1]-1)*6) % 7; // 0 - 7 $previous_month = (($vals[3] + 10) % 12)+1; // 1 - 12 $last_month_days = $dm[$previous_month-1]; // 28 -31 $next_month = ($vals[3] % 12)+1; // 1 - 12 $calendar = array(); // calcuate days of the previous month for($i = 0; $i < $first_day; $i++){ $calendar[] = array('day' => $last_month_days - ($first_day - $i - 1), 'month' => $previous_month, 'year' => (int)($previous_month != 12 ? $vals[4] : $vals[4]-1), 'specified' => false); } // calculate days of the current month for($i = 0; $i < $dm[$vals[3]-1]; $i++) { $calendar[] = array('day'=>$i+1, 'month'=>(int)$vals[3], 'year' => (int)($vals[4]), 'specified' => $vals[1] == $i+1 ? true : false); } // calculate days of the future month $length = count($calendar); if (($length % 7) > 0) { for ($i = 1; $i < ((7-($length % 7))+1); $i++){ $calendar[] = array('day'=>$i, 'month' => $next_month, 'year' => (int)($next_month != 1 ? $vals[4]: $vals[4]+1), 'specified' => false); } } return $calendar; }