Location: PHPKode > scripts > Calendar Generator > calendar-generator/calendar.class.php
<?php
/**
* +------------------------------------------------------------------------------+
* Dynamic Calendar Generator
* Author(s): Gobinath
* +------------------------------------------------------------------------------+
* Purpose of the Class:  Generate a Calendar Based on Use Input
* File Name : calendar.class.php
*/

class calendar{
          
   // Variable Declaration
   var $NOW;
   var $curMonth;
   var $curYear;
   var $curtxtMonth;
   var $curDate;
   var $curDay;
   var $thisMonth;
   var $TempMonth;
   var $totalDays;
   
  
   function calendar(){
   //Purpose : Constructor of the class and responsible for Variable Initialization 
        $this->curtxtMonth=array("","January","February","March","April","May","June","July","August","September","October","November","December");       
  	    $this->curDay=1;	
   }

   function AssignDate($Month,$Year){
   //Purpose : Assign Values to Global variable
 	    $NOW=getdate();     // get the Current server date.   
		if($Month > 12 || $Month < 1) { $Month = 0; $Year=0;} 
		if($Month == 0){
			$this->curMonth= $NOW[mon];
		}
		else{
			$this->curMonth= $Month;
		}
		if($Year == 0){
			$this->curYear = $NOW[year];
		}
		else{
			$this->curYear = $Year;
		}		
		$this->thisMonth=$this->curMonth;
	    $this->TempMonth=$NOW[month];
   }
   
   function GenerateCalendar(){
   //Purpose: Generate the Calendar based on the available Values
      $cellCount=0;
	  $TableCC=0;		
      echo "<table align=\"center\" cellpadding=\"1\" cellspacing=\"1\" border=\"0\">";
      echo "<tr bgcolor=\"#99CCFF\">";
      echo "<th colspan=\"7\">".$this->curtxtMonth[$this->curMonth]." - ".$this->curYear."</th>";
      echo "</tr>";
	  echo "<tr bgcolor=\"#99CCFF\">";
	  echo "<th><strong>Sun&nbsp;</strong></th><th><strong>Mon&nbsp;</strong></th><th><strong>Tue&nbsp;</strong></th>";
	  echo "<th><strong>Wed&nbsp;</strong></th><th><strong>Thu&nbsp;</strong></th><th><strong>Fri&nbsp;</strong></th>";
	  echo "<th><strong>Sat&nbsp;</strong></th>";
	  echo "</tr>";
      // Dynamic Calendar Generation Starts Here//
	  if($TableCC == 0 ) { 
	        echo " <tr align=\"center\" bgcolor=\"#FFFFFF\">"; 
	  }
	  echo " <td bgcolor=\"#FFFFFF\">";
	  while($this->curMonth==$this->thisMonth)
	  {
			$this->thisMonth=date("m",mktime(23,59,59,$this->curMonth,$this->curDay,$this->curYear));
			$this->totalDays=date("d",mktime(23,59,59,$this->curMonth,$this->curDay-1,$this->curYear));
			$this->curDay++;
	  }	  

	  $firstDayOfMonth = date("w",mktime(0,0,0,$this->curMonth,1,$this->curYear)); 
      if ($firstDayOfMonth > 0) { echo "<tr>"; }
	  for($cellCount=0;$cellCount<=$firstDayOfMonth-1;$cellCount++)
      {
    		echo "<td> </td>\r";
      }  
      for ($this->curDay=1;$this->curDay<=$this->totalDays;$this->curDay++)
      {
			 if ($cellCount == 0) { echo "<tr>\r"; }
			 if (($this->curDay == $this->curDate) && ($this->TempMonth == $this->curtxtMonth[$this->curMonth])){
					  echo "<td id=\"$this->curMonth-$this->curDay\">";
					  echo $this->curDay."</td>";
			 }
			else{
					 echo "<td id=\"$this->curMonth-$this->curDay\">";
					 echo $this->curDay."</td>";
			}
			$cellCount++;
			if ($cellCount == 7) { echo "</tr>\r"; $cellCount=0;}
      } // $this->curDay Loop		
 	  if ($cellCount > 0 && $cellCount < 7)
      {
    		for ($i=$cellCount; $i<7; $i++)
    		{
    			echo "<td>\r";
    		}
    		echo "</tr>\r";
      }
      $TableCC++;
      if ($TableCC == 3) { echo "</tr>\r"; $TableCC = 0; }		    	
      echo "</td>";
      echo "</table>";  
   }// GenerateCalendar() Function Ends here.
   
   
}
?>
Return current item: Calendar Generator