Easy month calendar builder
I had cause to build a month calendar recently. I took a look at what else was around (of course), including PEAR::CALENDAR (which is great, but a little over the top for my needs).
the class takes a year and month as arguments. a sample css file is also provided below
<?php /** * class to draw a month calendar * * example: $cal = new drawCalendarMonth (2008, 10); //for october 2008 $cal->draw(); * * the calendar design (ui and css) is taken from phpiCalendar */ if (isset($_GET['calendaraction']) && $_GET['calendaraction'] == "showMonth"){ $year = empty($_GET['calendarYear']) ? null : $_GET['calendarYear']; $month = empty($_GET['calendarYear']) ? null : $_GET['calendarMonth']; } else { $year = $month = null; } $cal = new drawCalendarMonth ($year, $month); $cal->draw(); class drawCalendarMonth{ /** * constructor function * @return void * @param $year Object[optional] * @param $month Object[optional] */ public function __construct($year=null, $month=null){ date_default_timezone_set('UTC'); $this->year = empty($year) ? $this->getCurrentYear() : $year; $this->month = empty ($month) ? $this->getCurrentMonth() : $month; $this->unix = strtotime("$this->year-$this->month-01"); $this->monthText = date('F', $this->unix); $this->numDays(); $this->getFirstDay(); //$this->getNextMonth(); //$this->getPrevMonth(); $this->getFirstBox(); $this->getNumRows(); } /** * method to build and output the calendar * * @return void */ public function draw(){ $this->curDay = $this->firstBox; $this->output= <<<HTML <div id="monthCalendar" class="calborder"> <div id="headerInfo" class="title"> <h1> {$this->monthText}, {$this->year}</h1> <h2><a href="{$_SERVER['PHP_SELF']}?calendaraction=showMonth&calendarMonth={$this->getPrevMonth()}&calendarYear={$this->getPrevYear()}"><<< Prev</a> <a href="{$_SERVER['PHP_SELF']}?calendaraction=showMonth&calendarMonth={$this->getNextMonth()}&calendarYear={$this->getNextYear()}">Next >>></a><h2> </div> <div id="monthTable"> <table cellpadding="2" class="monthback" id="calMonth"> <tr> <td class="dateback"> <span class="V9BOLD">Sunday</span> </td> <td class="dateback"> <span class="V9BOLD">Monday</span> </td> <td class="dateback"> <span class="V9BOLD">Tuesday</span> </td> <td class="dateback"> <span class="V9BOLD">Wednesday</span> </td> <td class="dateback"> <span class="V9BOLD">Thursday</span> </td> <td class="dateback"> <span class="V9BOLD">Friday</span> </td> <td class="dateback"> <span class="V9BOLD">Saturday</span> </td> </tr> HTML; for ($i=0; $i<$this->numRows; $i++){ $this->addRow(); } $this->output .= <<<HTML </table> </div> </div> HTML; echo $this->output; } /** * method to store the number of rows needed to build the calendar * @return */ private function getNumRows(){ $numBoxes = $this->firstDay + $this->numDays; $this->numRows = ceil($numBoxes/7); } /** * method to add rows to the calendar * @return void */ private function addRow(){ $this->output .= <<<HTML <tr> HTML; for ($i=0; $i<7; $i++){ $this->addDay(); } $this->output .= <<<HTML </tr> HTML; } /** * method to add a day to the table. * * if you want to add data to each day then here's the place to grab it. * @return */ private function addDay(){ $day = date('j', $this->curDay); $month = date ('n', $this->curDay); $class = $month == $this->month ? 'monthon' : 'monthoff'; $this->output .= <<<HTML <td class="$class" id="{$this->curDay}"> <div class="dayNumber"> <a class="dayNumberLink" href="day.php?action=showDayCalendargetdate={$this->curDay}">$day</a> </div> </td> HTML; $this->moveNext(); } /** * method to increment the day counter * @return */ private function moveNext(){ $this->curDay = strtotime("+1 day", $this->curDay); } /** * method to determine what day the top left box should display * * @return */ private function getFirstBox(){ if ($this->firstDay == 0 ){ //is a sunday $this->firstBox = $this->unix; } else { $this->firstBox = strtotime("-{$this->firstDay} days", $this->unix); } } /** * method to store the number of days in the current month * @return */ private function numDays(){ $this->numDays = date('t', $this->unix); } /** * method to store the day (sunday = 0) of the first day of the month * @return */ private function getFirstDay(){ $this->firstDay = date('w', $this->unix); } /** * method to provide the current month * @return */ private function getCurrentMonth(){ return date('m'); } /** * method to provide the next month * @return */ private function getNextMonth(){ return date('m', strtotime("+1 month",$this->unix)); } /** * method to provide the previous month * @return */ private function getPrevMonth(){ return date('m', strtotime("-1 month",$this->unix)); } /** * method to provide the year for the next month * @return */ private function getNextYear(){ return date('Y', strtotime("+1 month",$this->unix)); } /** * method to provide the year for previous month * @return */ private function getPrevYear(){ return date('Y', strtotime("-1 month",$this->unix)); } /** * method to provide the current year * @return */ private function getCurrentYear(){ return date('Y'); } } ?> |
and a css file (with thanks to phpICalendar)
/* Style sheet for the silver calendar (default) */ table, td {font: 11px Verdana, Arial, sans-serif; color: #000;} .calborder { background-color: #fff; border: 1px #A1A5A9 solid; width:735px; margin:0 auto;} .dateback { background-color: #eee; } /* Month View */ .monthback { background-color: #A1A5A9; } .monthoff { background-color: #F2F2F2; height: 105px; width: 105px; text-align: left; vertical-align: top; } .monthon { background-color: #F2F9FF; height: 105px; width: 105px; text-align: left; vertical-align: top; } /* Link colors and attributes */ /* This is the main link style */ a.psf { text-decoration: none; } a.psf:link { color: #0066FF; } a.psf:visited { color: #0066FF; } a.psf:active { color: #3366CC; } a.psf:hover { color: #000099; text-decoration: underline; } /* This is the link style for year months */ a.ps3 { text-decoration: none; } a.ps3:link { color: #000; } a.ps3:visited { color: #000; } a.ps3:active { color: #000; } a.ps3:hover { color: #000; text-decoration: underline; } /* Body attributes */ body { background-color: #E5E5E5; } /* CSS definitions for fonts */ .V9BOLD { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 9px; font-weight: 900; } |
August 11th, 2008 in
PHP, Sample Classes