PHP Calendar Class
This PHP Calendar Class generates a small calendar, and currently does not support event handling. Two lines need to be changed for use in any website: lines 81 and 83. They are the lines which make the links to different months, and currently they are set up to call a javascript function to load the next month with ajax. An article describing how to do that can be found here: Dynamically Loading Months from a Calendar. The rest of the script is easily modified to change the look and feel, and easier-to-modify variables may appear later.
The Code
- <?php
- class calendar {
- private $month;
- private $year;
- private $timestamp;
- private $nMonth;
- private $nYear;
- private $nTimestamp;
- private $pMonth;
- private $pYear;
- private $pTimestamp;
- private $build;
- private $firstDay;
- private function setNext(){
- if($this->month == 12){
- $this->nMonth = 1;
- $this->nYear = $this->year + 1;
- } else {
- $this->nMonth = $this->month + 1;
- $this->nYear = $this->year;
- }
- }
- private function setPrev(){
- if($this->month == 1){
- $this->pMonth = 12;
- $this->pYear = $this->year - 1;
- } else {
- $this->pMonth = $this->month - 1;
- $this->pYear = $this->year;
- }
- }
- private function getFirstDay(){
- }
- public function __construct($timestamp){
- $this->timestamp = $timestamp;
- $this->setNext();
- $this->setPrev();
- $this->printCalendar();
- }
- public function printCalendar(){
- $this->build = '<table summary="Calendar">';
- $this->build .= '<thead>';
- $this->build .= '<tr>';
- $this->build .= '<th abbr="Sunday" scope="col" title="Sunday">S</th>';
- $this->build .= '<th abbr="Monday" scope="col" title="Monday">M</th>';
- $this->build .= '<th abbr="Tuesday" scope="col" title="Tuesday">T</th>';
- $this->build .= '<th abbr="Wednesday" scope="col" title="Wednesday">W</th>';
- $this->build .= '<th abbr="Thursday" scope="col" title="Thursday">T</th>';
- $this->build .= '<th abbr="Friday" scope="col" title="Friday">F</th>';
- $this->build .= '<th abbr="Saturday" scope="col" title="Saturday">S</th>';
- $this->build .= '</tr>';
- $this->build .= '</thead>';
- $this->build .= '<tfoot>';
- $this->build .= '<tr>';
- $this->build .= '<td class="pad"> </td>';
- $this->build .= '</tr>';
- $this->build .= '</tfoot>';
- $this->build .= '<tbody>';
- $this->build .= '<tr>';
- //first row
- $this->firstDay = $this->getFirstDay();
- $start = $prevMonthDays - $this->firstDay;
- $genCounter = 1;
- for($start;$start<$prevMonthDays;$start++){
- $this->build .= '<td>'.($start + 1).'</td>';
- $genCounter++;
- }
- for($d=1;$d<=$totalDays;$d++){
- $this->build .= '<td id="today">'.$d.'</td>';
- } else {
- $this->build .= '<td>'.$d.'</td>';
- }
- if($genCounter % 7 == 0 && $d != $totalDays){
- $this->build .= '</tr><tr>';
- }
- $genCounter++;
- }
- $start = 1;
- $genCounter--;
- while($genCounter % 7 != 0){
- $this->build .= '<td>'.$start.'</td>';
- $start++;
- $genCounter++;
- }
- $this->build .= '</tr>';
- $this->build .= '<tbody>';
- $this->build .= '</table>';
- }
- }
- ?>
The Demo
A working demo can be seen with the article that explains how to load months with Ajax, and can be found here: Dynamically Loading Months from a Calendar. For the sake of not having things in too many places it has been removed from this page.
The Download
| Filename | calendar.class.php |
|---|---|
| Filesize | 3632 bytes |
| Filetype | PHP Script |
| MD5 checksum | f3a900e663229f659bd896e85128bea9 |
| License | © 2009 by Sculch, LLC. under the MIT License |
