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

  1. <?php
  2. class calendar {
  3.  
  4.     private $month;
  5.     private $year;
  6.     private $timestamp;
  7.    
  8.     private $nMonth;
  9.     private $nYear;
  10.     private $nTimestamp;
  11.     private $pMonth;
  12.     private $pYear;
  13.     private $pTimestamp;
  14.    
  15.     private $build;
  16.     private $firstDay;
  17.  
  18.     private function setNext(){
  19.        
  20.         if($this->month == 12){
  21.             $this->nMonth = 1;
  22.             $this->nYear = $this->year + 1;
  23.         } else {
  24.             $this->nMonth = $this->month + 1;
  25.             $this->nYear = $this->year;
  26.         }
  27.        
  28.         $this->nTimestamp = mktime(0,0,0,$this->nMonth,1,$this->nYear);
  29.        
  30.     }
  31.    
  32.     private function setPrev(){
  33.        
  34.         if($this->month == 1){
  35.             $this->pMonth = 12;
  36.             $this->pYear = $this->year - 1;
  37.         } else {
  38.             $this->pMonth = $this->month - 1;
  39.             $this->pYear = $this->year;
  40.         }
  41.        
  42.         $this->pTimestamp = mktime(0,0,0,$this->pMonth,1,$this->pYear);
  43.        
  44.     }
  45.    
  46.     private function getFirstDay(){
  47.        
  48.         return date("w",mktime(0,0,0,$this->month,1,$this->year));
  49.        
  50.     }
  51.    
  52.     public function __construct($timestamp){
  53.        
  54.         $this->timestamp = $timestamp;
  55.         $this->month = date("n",$this->timestamp);
  56.         $this->year = date("Y",$this->timestamp);
  57.         $this->setNext();
  58.         $this->setPrev();
  59.         $this->printCalendar();
  60.        
  61.     }
  62.    
  63.     public function printCalendar(){
  64.        
  65.         $this->build = '<table summary="Calendar">';
  66.         $this->build .= '<caption>'.date("F Y",$this->timestamp).'</caption>';
  67.         $this->build .= '<thead>';
  68.         $this->build .= '<tr>';
  69.         $this->build .= '<th abbr="Sunday" scope="col" title="Sunday">S</th>';
  70.         $this->build .= '<th abbr="Monday" scope="col" title="Monday">M</th>';
  71.         $this->build .= '<th abbr="Tuesday" scope="col" title="Tuesday">T</th>';
  72.         $this->build .= '<th abbr="Wednesday" scope="col" title="Wednesday">W</th>';
  73.         $this->build .= '<th abbr="Thursday" scope="col" title="Thursday">T</th>';
  74.         $this->build .= '<th abbr="Friday" scope="col" title="Friday">F</th>';
  75.         $this->build .= '<th abbr="Saturday" scope="col" title="Saturday">S</th>';
  76.         $this->build .= '</tr>';
  77.         $this->build .= '</thead>';
  78.  
  79.         $this->build .= '<tfoot>';
  80.         $this->build .= '<tr>';
  81.         $this->build .= '<td abbr="'.date("F",$this->pTimestamp).'" colspan="3" id="prev"><a href="#" title="" onclick="loadCalendar(\''.$this->pMonth.'\',\''.$this->pYear.'\');return false;">&laquo; '.date("M",$this->pTimestamp).'</a></td>';
  82.         $this->build .= '<td class="pad">&nbsp;</td>';
  83.         $this->build .= '<td abbr="'.date("F",$this->nTimestamp).'" colspan="3" id="next"><a href="#" title="" onclick="loadCalendar(\''.$this->nMonth.'\',\''.$this->nYear.'\');return false;">'.date("M",$this->nTimestamp).' &raquo;</a></td>';
  84.         $this->build .= '</tr>';
  85.         $this->build .= '</tfoot>';
  86.        
  87.         $this->build .= '<tbody>';
  88.         $this->build .= '<tr>';
  89.        
  90.         //first row
  91.         $this->firstDay = $this->getFirstDay();
  92.         $prevMonthDays = date("t",$this->pTimestamp);
  93.        
  94.         $start = $prevMonthDays - $this->firstDay;
  95.        
  96.         $totalDays = date("t",$this->timestamp);
  97.         $genCounter = 1;
  98.        
  99.         for($start;$start<$prevMonthDays;$start++){
  100.            
  101.             $this->build .= '<td>'.($start + 1).'</td>';
  102.             $genCounter++;
  103.        
  104.         }
  105.        
  106.         $today = mktime(0,0,0,date("n"),date("j"),date("Y"));;
  107.        
  108.         for($d=1;$d<=$totalDays;$d++){
  109.            
  110.             if(mktime(0,0,0,$this->month,$d,$this->year) == $today){
  111.                 $this->build .= '<td id="today">'.$d.'</td>';
  112.             } else {
  113.                 $this->build .= '<td>'.$d.'</td>';
  114.             }
  115.            
  116.             if($genCounter % 7 == 0 && $d != $totalDays){
  117.                
  118.                 $this->build .= '</tr><tr>';
  119.            
  120.             }
  121.            
  122.             $genCounter++;
  123.            
  124.         }
  125.        
  126.         $start = 1;
  127.         $genCounter--;
  128.         while($genCounter % 7 != 0){
  129.             $this->build .= '<td>'.$start.'</td>';
  130.             $start++;
  131.             $genCounter++;
  132.         }
  133.        
  134.        
  135.         $this->build .= '</tr>';
  136.         $this->build .= '<tbody>';
  137.        
  138.         $this->build .= '</table>';
  139.         echo $this->build;
  140.  
  141.        
  142.     }
  143.  
  144. }
  145. ?>

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

Download Now

TAGS: PHP