/** * Creates a matrix with 7 columns, one per day of week, and as many rows (weeks) as necessary. * Every cell contains a dictionary with the wotd, the definition and other info. */ function createCalendar($year, $month) { $days = listDaysOfMonth($year, $month); $today = date('Y-m-d'); $calendar = array(); // Pad beginning $startDow = date('N', strtotime("{$year}-{$month}-01")); for ($i = 1; $i < $startDow; $i++) { $calendar[] = array(); } // Create a record per day foreach ($days as $i => $date) { $wotd = WordOfTheDay::get_by_displayDate($date); $wotdr = $wotd ? WordOfTheDayRel::get_by_wotdId($wotd->id) : null; $def = $wotdr ? Definition::get_by_id($wotdr->refId) : null; $visible = $def && ($date <= $today || util_isModerator(PRIV_WOTD)); $calendar[] = array('wotd' => $wotd, 'def' => $def, 'visible' => $visible, 'dayOfMonth' => $i + 1); } // Pad end while (count($calendar) % 7 != 0) { $calendar[] = array(); } // Wrap 7 records per line $weeks = array(); while (count($calendar)) { $weeks[] = array_splice($calendar, 0, 7); } return $weeks; }
function createCalendar($year, $month) { $days = listDaysOfMonth($year, $month); $today = date('Y-m-d'); $words = WordOfTheDay::getArchiveWotD($year, $month); $inv = array(); foreach ($words as $k => $v) { $inv[$v->displayDate] = $k; } $new_words = array(); foreach ($days as $day) { if ($day <= $today && array_key_exists($day, $inv)) { $new_words[] = $words[$inv[$day]]; } else { $new_words[] = WotDArchive::setOnlyDate($day); } } return $new_words; }