Jump to content

Calendar Class


phpSensei

Recommended Posts

Where else do i post this?

 

calendar.class.php


////////////////////////////////////////////////
////////////////////////////////////////////////
//// Title:  PHPsensei Calendar 1.0	////////
//// Author: Nasir Dowlatkhahi//////////////////
//// Installation: -- N/A///////////////////////
//// Licensing: General Public License (GNU)////	
//// History: -- None///////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////

class Calendar{

		private  $_currentDay;    // Contains the current day
			private  $_currentMonth; // Contains the current Month
		private	 $_currentYear; // Contains the current year
		private  $_daysInMonth; // Contains the number of days in month 
		private  $_zerosInMonth;


		function __construct(){ // Our construction

			$this->_currentDay   = (isset($_GET['d'])) ? $this->filter_var($_GET['d'],  'day')   :   $this->getCurrentDate('j');
			$this->_currentMonth = (isset($_GET['m'])) ? $this->filter_var($_GET['m'],'month')   :   $this->getCurrentDate('m');
			$this->_currentYear  = (isset($_GET['y'])) ? $this->filter_var($_GET['y'], 'year')   :   $this->getCurrentDate('Y');		 
			$this->_daysInMonth  = $this->getMonthDays($this->_currentMonth,$this->_currentYear);

				/** Testing Purpose Only, You can Delete This Comment **/
				/** print $this->_currentDay   . '<br \>'; 
				/** print $this->_currentMonth . '<br \>'; 
				/** print $this->_currentYear  . '<br \>';	
				/** print $this->_daysInMonth  . '<br \>'; 	
				/** Testing Purpose Only, You can Delete This Comment **/

		}

	    public function getMonthDays($month,$year){ // Get how many dats in the month

				return date('t', mktime(0,0,0,$month, 1, $year));
		}

	    public function getCurrentDate($format){ // Get Current Date

				return date($format,time());

		}
		public function getFirstDay($date){


				return date('D', $date);

		}
		public function filter_var($val, $format){

				switch($format){

					case "day":  
					// Numeric Value
					// No more than 2 didgits
					// Strip all html and tags
					if(is_numeric($val) && preg_match('/^[0-9]{2}$/',$val)){


								return strip_tags(trim($val));

					}else{

							    return $this->getCurrentDate('j'); 

					}						
					break; 
						case "month": 
					// Numeric Value
					// No more than 2 didgits
					// Strip all html and tags
					if(is_numeric($val) && preg_match('/^[0-9]{2}$/',$val)){

									return strip_tags(trim($val));

					}else{

							    return $this->getCurrentDate('m'); 

					}						
					break; 
						case "year": 
					// Numeric Value
					// No more than 4 didgits
					// Strip all html and tags
					if(is_numeric($val) && preg_match('/^[0-9]{4}$/',$val)){


								return strip_tags(trim($val));

					}else{

							    return $this->getCurrentDate('Y'); 

					}						
					break; 

				}			
		}
		public function leadingZeros($weekname){

			$val = '';
				switch($weekname){

					case "Sun": $val = 0; break; 
						case "Mon": $val = 1; break; 
						case "Tue": $val = 2; break; 
						case "Wed": $val = 3; break; 
						case "Thu": $val = 4; break; 
						case "Fri": $val = 5; break; 
					case "Sat": $val = 6; break; 

				}
			return $val;
		}

		public function getMonthNames($val){

				switch($val){

					case "1": $val = 'January';  break; 
						case "2": $val = 'February'; break; 
						case "3": $val = 'March';    break; 
						case "4": $val = 'April';    break; 
						case "5": $val = 'May';      break; 
						case "6": $val = 'June';     break; 
					case "7": $val = 'July';     break; 
					case "8": $val = 'August';   break; 
					case "9": $val = 'September';break; 
					case "10": $val = 'October'; break; 
					case "11": $val = 'November'; break; 
					case "12": $val = 'December'; break; 
				}
			return $val;			
		}
     	    public function displayCalendar(){ // This is where the magic happens
			// Some variables for switching the calendar year and month
			$prev_month = $this->_currentMonth - 1;
			$prev_year  = $this->_currentYear;

			$next_month = $this->_currentMonth + 1;
			$next_year  = $this->_currentYear;

				if($prev_month < 1 ){

					$prev_year = $prev_year - 1;
					$prev_month = 12;

				}

				if($prev_month < 10){

					$prev_month = '0'.$prev_month;

				}

				if($next_month > 12 ){

					$next_year = $next_year + 1;
					$next_month = 1;

				}
				if($next_month >= 1 && $next_month < 10 ){

					$next_month = '0'.$next_month;

				}
				// Build the table header
			$header = '<table width="80%" border="0" cellspacing="0" cellpadding="0">
  						   <tr>
  						     <td align="center" height="23" valign="middle" id="main_title"><a href="index.php?y='.$prev_year.'&m='.$prev_month.'"><<</a> '.$this->getMonthNames($this->_currentMonth).', '.$this->_currentYear.' <a href="index.php?y='.$next_year.'&m='.$next_month.'">>></a></td>
     			  </tr>
  						   <tr>
  						     <td align="center" height="20" valign="middle" id="main_title2"><table width="100%" border="0" cellspacing="1" cellpadding="0">
                               <tr>
                                 <td width="15%" height="22" align="center" id="weekend_title">Sun</td>
                                 <td width="15%" height="22" align="center" id="weekend_title">Sat</td>
                                 <td width="15%" height="22" align="center" id="weekend_title">Mon</td>
                                 <td width="15%" height="22" align="center" id="weekend_title">Tue</td>
                                 <td width="15%" height="22" align="center" id="weekend_title">Wen</td>
                                 <td width="15%" height="22" align="center" id="weekend_title">Thu</td>
                                 <td width="15%" height="22" align="center" id="weekend_title">Fri</td>
                               </tr>
                             </table></td>
		      </tr>
  						   <tr>
    					   <td height="114" align="left" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="3">';
					   
			// Build the table footer
			$footer = '</table></td>
  						   </tr>
					   </table>
						<tr>';

						// Date Formats
						$first_day = date('D', mktime(0,0,0,$this->_currentMonth, 1, $this->_currentYear));
						$this->_zerosInMonth = $this->leadingZeros($first_day);
						// Our Counters
						$column_counter = 1;
						$num_columns    = 7;
						$total_counter  = 0;
						//Booleans
						$print_days     = false;				


						// Counters & Date Formats Continued
						$prev_year_last = ($this->getMonthDays($this->_currentMonth - 1, $this->_currentYear - 1))-$this->_zerosInMonth + 1;

				// Go ahead and print the header of the table
				print $header;

				// Lets print the blanks first
				while($this->_zerosInMonth > 0){
						// Print the blanks
						print '<td height="20" align="left" valign="middle" id="month_blanks">'.$prev_year_last.'</td>';
						$prev_year_last++;

					$this->_zerosInMonth = $this->_zerosInMonth - 1;
					$column_counter++;
					$total_counter++;

				}

				for($i=1;$i<=$this->_daysInMonth;$i++){

					// Keep printing new columns as long as its less than the total number of columsn (7)
					if($column_counter <= $num_columns){

						if($i == $this->_currentDay){ $td_class = 'month_day_select'; }else{ $td_class = 'month_days'; }
						print '<td height="20" align="left" valign="middle" id="'.$td_class.'">'.$i.'</td>';

					}

					// Add to our column counter
					$column_counter++;
					$total_counter++;

					// If more than 7 columns have been printed, create a new row and start over again
					if($column_counter > 7){
						print '</tr><tr>';
						$column_counter = 1;

					  }

					}

				// The remaining Blanks
				if((42-$total_counter) != 0){
						for($i=1;$i<=(42-$total_counter);$i++){
						print '<td height="20" align="left" valign="middle" id="month_blanks">'.$i.'</td>';

						$column_counter++;

						if($column_counter > 7){

								print '</tr><tr>';
								$column_counter = 1;

							}
					    }

					}

			// Print the footer
			print $footer;
		}
}

// COPYRIGHT @ PHPsensei.com - DO NOT DISTRIBUTE THIS CODE << IGNORE THIS LINE
?>

 

calendar.css

 

@charset "utf-8";
/* CSS Document */
#main_title{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;
color:#FFFFFF;
background-color:#D15252;
background-image:url(../html/images/cal_bg_gradient.jpg);

}
#main_title a{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;
color:#FFFFFF;
text-decoration:none;

}
#main_title a:hover{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;
color:#FFFFFF;
text-decoration:none;
}
#main_title a:active{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;
color:#FFFFFF;
text-decoration:none;

}
#main_title a:visited{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;
color:#FFFFFF;
text-decoration:none;
}
#month_days{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;

}
#month_day_select{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#333333;
font-weight:bold;

	border: solid 1px #D15252
}
#month_blanks{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#999999;
}
#month_title{

}
#weekend_title{

font-family:Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color:#333333;
font-weight:bold;
background-color:#EFEFEF;
}



Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.