Version: 1.0
Type: Class
Category: Calendars/Dates
License: GNU General Public License
Description: Simple class with clear and documented code, which will help you to both to understand the algorithm of the building the calendar and use this idea in your application.
<?php /** *Class which contains functions to build Calendar for a definite month *General algorithm is as following: *We Build the array of days of month and put 0 where there is no value and then *display it in a way we want. We can simply display it, we can add news, appointments * and whatever you like to the every date of the month. The aim oth this class is to show * how to build the calendar itself, not how to display or work with it */ class Calendar { /** *Number of the month for which the calendar is built */ var $month; /** *Number of the year */ var $year; /** *Current day. It is used for building the calendar */ var $day = 0; /** *Maximum days in current month */ var $maxday; /** *Array of the month's days */ var $monthArray = array(); /** *Constructor of the class. In it we get the shift towards *current month from address line bye the $_GET method *Then we sum it with the current month. If the number is greater * than 12, we add one year and subtract 12 months from the month value *If the number less than zero, we behave quite opposite - add 12 months * and subtract one year. *If everything is OK, we simply put current year in the year property of *the object */ function Calendar() { $this->month = date('n')+(integer)$_GET['smon']; if ( $this->month >12 ) { $this->month = $this->month - 12; $this->year = date('Y'); } else { if ( $this->month < 0 ) { $this->month = 12 + $this->month;//as $this->month<0, we add it $this->year = date('Y') - 1; } else { $this->year = date('Y'); } } $this->_getMaxDays(); } /** *Function calculates number of the days in the month *@return integer $maxday Result is put to the property of the object */ function _getMaxDays() { $time=getdate(mktime(0,0,0,$this->month+1, 1, $this->year)-1); $this->maxday = $time['mday']; } /** *Function builds the week of a definite month *<p> *It takes all arguments from the object's properties *<p> *It behaves in the following way: *<p> * If the value of the current day is zero, then it is the first week of the * month and we move towards cycling the number of days of the week up to the * number of the week for the first day of the month. When we reach it, we begin * to add day of the month to the week array and increment the number of the day *<p> * If the Day value is non-zero, then we increment the number of the day and then * check, if it is less of equal to the maximal value of the day in this month. * If it is greater, we put zeroes to the week array, otherwise we put number of * the day */ function _buildWeek($begday) { if ( $this->day == 0 ) { for ($i = 0; $i<7; $i++) { if ( $i >= $begday ) { $this->day++; $week[$i] = $this->day; } else { $week[$i] = 0; } } } else { for ($i = 0; $i<7; $i++) { $this->day++; if ( $this->day <= $this->maxday ) { $week[$i] = $this->day; } else { $week[$i] = 0; } } } return $week; } /** *Function builds month itself *It cycles from 0 to 6 weeks (maximum) and builds the array of month's days */ function _BuildMonth() { /** *Here if we subtract 1, week will begin with Monday, if we will not, Sunday will *be the first day */ $startday = (integer)strftime('%w', mktime(0,0,0,$this->month, 1, $this->year)) - 1; for ( $i = 0; $i < 7; $i++ ) { $this->monthArray[$i] = $this->_buildWeek($startday); if ( $this->monthArray[$i][6] == 0 ) { break; } } } /** *Function displays the calendar to the user. *It is only an example. Here we cycle through weeks, then view each day of the week *If there is not "0", then we display the number, otherwise, we echo */ function DisplayMonth() { $this->_BuildMonth(); $weeks=sizeof($this->monthArray); echo "<table>n"; for ( $i = 0; $i < $weeks; $i++ ) { echo "<tr>n"; for ( $j = 0; $j < 7; $j++) { echo "<td>n"; if ($this->monthArray[$i][$j] != 0) { echo $this->monthArray[$i][$j]."n"; } else { echo " n"; } echo "</td>n"; } echo "</tr>n"; } echo "</table>n"; } } ?>