Location: PHPKode > scripts > EasyPhpEventCalendar Class > easyphpeventcalendar/inc/easyphpeventcalendar.class.php
<?php

/**

EasyPhpEventCalendar Class version 1.0.2

Copyright (c) 2009-2011 JF Nutbroek <hide@address.com>
For more information visit http://wwww.mywebmymail.com

Permission to use, copy, modify, and/or distribute this software for any
purpose without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

class easyphpeventcalendar {
    
	/**
	* URL to gfx directory with calendar backgrounds
	*
	* @var string
	*/        
	public $Gfxurl;
	/**
	* Days; array with 7 string values representing the days
	* [0]: Sunday
	* [1]: Monday
	* [2]: Tuesday
	* [3]: Wednesday
	* [4]: Thursday
	* [5]: Friday
	* [6]: Saturday
	*
	* @var array
	*/        
	public $Days;
	/**
	* Months; array with 12 string values representing the months
	* [0]: January
	* [1]: February
	* [2]: March
	* [3]: April
	* [4]: May
	* [5]: June
	* [6]: July
	* [7]: August
	* [8]: September
	* [9]: October
	* [10]: November
	* [11]: December
	*
	* @var array
	*/        
	public $Months;        
	/**
	* Events; array with arrays representing the events
	* [0]: array ('YYYYMMDD','Event 1 as text with html markup')
	* [1]: array ('YYYYMMDD','Event 2 as text with html markup')
	* [2]: etc.
	*
	* A period is marked with 2 full dates indicating start and end:
	* [0]: array ('YYYYMMDDYYYYMMDD','Event 1 from start to end as text with html markup')
	* [1]: array ('YYYYMMDDYYYYMMDD','Event 2 from start to end as text with html markup')
	* [2]: etc.    
	*
	* Single events and an event periods can be mixed 
	*
	* @var array
	*/        
	public $Events;
	/**
	* Hide days from the previous/next month in the display (greyed out in normal mode)
	*
	* @var boolean
	*/        
	public $Hide;        
	
	
	/**
	* Class constructor
	*
	*/    
	public function __construct() {
         
		$this->Gfxurl  = 'gfx';           
		$this->Days    = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
		$this->Months  = array('January','February','March','April','May','June','July','August','September','October','November','December');
		$this->Events  = array();
		$this->Hide    = false;
		
	}

	/**
	* Creates Event Calendar CSS
	*
	*/    
	public function CreateEventCalendarCSS() {
        
		$this->Gfxurl = rtrim($this->Gfxurl, '/');
		echo "<style type=\"text/css\">\n";                    
		echo ".eventtable {\n";
		echo "    text-align        : center;\n";
		echo "    font-family       : Verdana, Helvetica, Arial, sans-serif;\n";
		echo "    font-size         : 11px;\n";
		echo "    color             : #000000;\n";
		echo "    border-collapse   : separate;\n"; 
		echo "    border-spacing    : 2px;\n";
		echo "    width             : 410px;\n";                
		echo "}\n";                        
		echo ".eventweekmonth {\n";
		echo "    background-image  : url('$this->Gfxurl/weekmonth.gif');\n";
		echo "    background-repeat : no-repeat;\n";                
		echo "    height            : 25px;\n";
		echo "    width             : 40px;\n";
		echo "}\n";
		echo ".eventweekmonthactive {\n";  
		echo "    background-image  : url('$this->Gfxurl/weekmonth_event.gif');\n";
		echo "    background-repeat : no-repeat;\n";                
		echo "    height            : 25px;\n";
		echo "    width             : 40px;\n";                
		echo "    font-weight       : bold;\n";
		echo "}\n";                
		echo ".eventweekend {\n";
		echo "    background-image  : url('$this->Gfxurl/weekend.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                
		echo "}\n";
		echo ".eventweekendactive {\n";
		echo "    background-image  : url('$this->Gfxurl/weekend_event.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                    
		echo "    font-weight       : bold;\n";
		echo "}\n";
		echo ".eventweekendactiveperiod {\n";
		echo "    background-image  : url('$this->Gfxurl/weekend_event_period.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                    
		echo "    font-weight       : bold;\n";
		echo "}\n";		
		echo ".eventday {\n";  
		echo "    background-image  : url('$this->Gfxurl/day.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";    
		echo "}\n";                
		echo ".eventdayactive {\n";  
		echo "    background-image  : url('$this->Gfxurl/day_event.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                    
		echo "    font-weight       : bold;\n";
		echo "}\n";
		echo ".eventdayactiveperiod {\n";  
		echo "    background-image  : url('$this->Gfxurl/day_event_period.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                    
		echo "    font-weight       : bold;\n";
		echo "}\n";		
		echo ".eventdaydisabled {\n";  
		echo "    background-image  : url('$this->Gfxurl/day_disabled.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                    
		echo "    color             : #969696;\n";
		echo "}\n";
		echo ".eventweekenddisabled {\n";  
		echo "    background-image  : url('$this->Gfxurl/weekend_disabled.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 19px;\n";
		echo "    width             : 40px;\n";                    
		echo "    color             : #969696;\n";
		echo "}\n";
		echo ".eventdetails {\n";  
		echo "    background-image  : url('$this->Gfxurl/eventdetails.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    text-align        : left;\n";
		echo "    vertical-align    : top;\n";
		echo "    height            : 160px;\n";
		echo "    width             : 304px;\n";                    
		echo "    padding           : 5px;\n";        
		echo "}\n";                  
		echo ".eventcalendartitle {\n";
		echo "    background-image  : url('$this->Gfxurl/title.gif');\n";
		echo "    background-repeat : no-repeat;\n";
		echo "    height            : 25px;\n";
		echo "    width             : 304px;\n";                    
		echo "    font-weight       : bold;\n";
		echo "}\n";
		echo "</style>\n";
        
	}

	/**
	* Creates Event Calendar
	*
	*/    
	public function CreateEventCalendar() {

		// Get current date
		$date = time();
		$month = date('m', $date);
		$year = date('Y', $date);

		// Left column with months
		echo "<table class=\"eventtable\"><tr><td style=\"vertical-align : top;width : 44px;\">\n";
		echo "<table style=\"vertical-align : top;border-collapse : separate;border-spacing : 2px;width : 44px;\">\n";
		echo "<tr><td style=\"height : 25px;\">&nbsp;</td></tr>\n";
		if ($this->eventcheck(1)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div1')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[0], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(2)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div2')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[1], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(3)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div3')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[2], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(4)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div4')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[3], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(5)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div5')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[4], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(6)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div6')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[5], 0, 3) . "</td></tr>\n";
		echo "</table>\n";
		echo "</td><td style=\"vertical-align : top;width : 308px;\">\n";

		// Add Events
		foreach ($this->Events as $key => $event) {
			$eventmonth = $eventmonth_end = intval(substr($event[0], 4, 2));
			$eventyear = $eventyear_end = intval(substr($event[0], 0, 4));
			$eventday = $eventday_end = intval(substr($event[0], 6, 2));
			// Check if we are working with an event period
			if (strlen($event[0]) == 16) {
				$eventyear_end = intval(substr($event[0], 8, 4));
				$eventmonth_end = intval(substr($event[0], 12, 2));
				$eventday_end = intval(substr($event[0], 14, 2));
			}
			if (checkdate($eventmonth, $eventday, $eventyear) && checkdate($eventmonth_end, $eventday_end, $eventyear_end)) {
				$day_no = date('N', mktime(0, 0, 0, $eventmonth, $eventday, $eventyear));
				$month_no = date('n', mktime(0, 0, 0, $eventmonth, $eventday, $eventyear)) - 1;
				$day_no_end = date('N', mktime(0, 0, 0, $eventmonth_end, $eventday_end, $eventyear_end));
				$month_no_end = date('n', mktime(0, 0, 0, $eventmonth_end, $eventday_end, $eventyear_end)) - 1;				
				if ($day_no == 7) {$day_no = 0;}
				if ($day_no_end == 7) {$day_no_end = 0;}
				if (strlen($event[0]) == 16) {
					if ($eventyear_end != $eventyear) {
						$date = substr($this->Days[$day_no], 0, 3) . ' ' . $eventday . ' ' . substr($this->Months[$month_no], 0, 3) . ' ' . $eventyear . ' / ' . substr($this->Days[$day_no_end], 0, 3) . ' ' . $eventday_end . ' ' . substr($this->Months[$month_no_end], 0, 3) . ' ' . $eventyear_end;
					} else if ($eventmonth_end != $eventmonth) {
						$date = substr($this->Days[$day_no], 0, 3) . ' ' . $eventday . ' ' . $this->Months[$month_no] . ' / ' . substr($this->Days[$day_no_end], 0, 3) . ' ' . $eventday_end . ' ' . $this->Months[$month_no_end]   . ' ' . $eventyear;
					} else {
						$date = $this->Days[$day_no] . ' ' . $eventday . ' / ' . $this->Days[$day_no_end] . ' ' . $eventday_end . ' ' . $this->Months[$month_no] . ' ' . $eventyear;
					}
				} else {
					$date = $this->Days[$day_no] . ' ' . $eventday . ' ' . $this->Months[$month_no] . ' ' . $eventyear;
				}
				echo "<div id=\"div" . ($key + 13) ."\" style=\"display: none\">\n";
				echo "<table style=\"width : 308px;vertical-align : top;border-collapse : separate;border-spacing : 2px;\"><tr><td class=\"eventcalendartitle\">$date</td></tr><tr><td class=\"eventdetails\" onclick=\"expandMonth('div$eventmonth')\" onmouseover=\"this.style.cursor='pointer';\">" . $event[1] . "</td></tr></table>\n";
				echo "</div>\n";
			}
		}
       
		// Add Months
		for ($m = 1; $m < 13; $m++) {
			if ($m == $month) {
				echo "<div id=\"div$m\">\n";
				$this->eventcalendar(mktime(0, 0, 0, $m, 1, $year));
				echo "</div>\n";
			} else {
				if ($m < $month) {$i = 1;} else {$i = 0;}
				echo "<div id=\"div$m\" style=\"display: none\">\n";
				$this->eventcalendar(mktime(0, 0, 0, $m, 1, $year + $i));
				echo "</div>\n";        
			}
		}

		// Right column with months
		echo "</td><td style=\"vertical-align : top;width : 44px;\">\n";
		echo "<table style=\"width : 44px;vertical-align : top;border-collapse : separate;border-spacing : 2px;\">\n";
		echo "<tr><td style=\"height : 25px;\">&nbsp;</td></tr>\n";
		if ($this->eventcheck(7)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div7')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[6], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(8)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div8')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[7], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(9)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div9')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[8], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(10)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div10')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[9], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(11)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div11')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[10], 0, 3) . "</td></tr>\n";
		if ($this->eventcheck(12)) {$class = 'eventweekmonthactive';} else {$class = 'eventweekmonth';}
		echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div12')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[11], 0, 3) . "</td></tr>\n";
		echo "</table>\n";
		echo "</td></tr></table>\n";
                
	}
        
	/**
	* Check if month has an event and event is not in the past (last year)
	*
	*/
	private function eventcheck($month) {
    
		$year_now = intval(date('Y', time()));
		$month_now = intval(date('m', time()));
		foreach ($this->Events as $key => $event) {
			if (strlen($event[0]) == 16) {
				$event_start = intval(substr($event[0], 4, 2)); 
				$event_end = intval(substr($event[0], 12, 2));
				if ($event_end < $event_start) {
					if ($month >= $event_start && $month <= 12 && intval(substr($event[0], 8, 4)) >= $year_now)
						return true;
					if ($month >= 1 && $month <= $event_end && intval(substr($event[0], 8, 4)) >= $year_now)
						return true;				
				} else if ($month >= $event_start && $month <= $event_end && $month >= $month_now && intval(substr($event[0], 0, 4)) >= $year_now) {
					return true;
				}
			} else {
				if (intval(substr($event[0], 4, 2)) == $month && intval(substr($event[0], 0, 4)) >= $year_now)
					return true;
			}
		}
		return false;
			
	}
            
	/**
	* Print the month
	*
	*/
	private function eventcalendar($date) {
    
		$day = date('d', $date);
		$month = date('m', $date);
		$year = date('Y', $date);
		$first_day = mktime(0, 0, 0, $month, 1, $year);
		$title = $this->Months[date('n', mktime(0, 0, 0, $month, $day, $year)) - 1]; 
		$days_in_month = cal_days_in_month(0, $month,$year);
		$day_of_week = date('N', $first_day);
		if ($day_of_week == 7) {
			$blank = 0;
		} else {
			$blank = $day_of_week;
		}
		echo "<table style=\"vertical-align : top;border-collapse : separate;border-spacing : 2px;width : 308px;\">";
		echo "<tr><td colspan=\"7\" class=\"eventcalendartitle\">$title $year</td></tr>";
		echo "<tr><td class=\"eventweekmonth\">". substr($this->Days[0], 0, 3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[1], 0, 3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[2], 0, 3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[3], 0, 3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[4], 0, 3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[5], 0, 3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[6], 0, 3) . "</td></tr>";
		if (($month - 1) == 0) {
			$prevmonth = 1;
			$prevyear = ($year - 1);
		} else {
			$prevmonth = ($month - 1);
			$prevyear = $year;
		}
		$day_prevmonth = cal_days_in_month(0, $prevmonth, $prevyear) - ($blank - 1);
		echo "<tr>";
		$day_num = 1;
		$day_count=1;
		$datenow = time();
		$monthnow = date('n', $datenow);
		$yearnow = date('Y', $datenow);
		$daynow = date('j', $datenow);
		if ($blank > 0) {
			if ($this->Hide)
				echo "<td>&nbsp;</td>";
			else
				echo "<td class=\"eventweekenddisabled\">$day_prevmonth</td>";
			$blank = $blank - 1;
			$day_count++;
			$day_prevmonth++;
		} 
		while ($blank > 0) {
			if ($this->Hide)
				echo "<td>&nbsp;</td>";
			else
				echo "<td class=\"eventdaydisabled\">$day_prevmonth</td>";
			$blank = $blank - 1;
			$day_count++;
			$day_prevmonth++;
		} 
		while ($day_num <= $days_in_month) {    
			$class = "class=\"eventday\"";
			$id = 0;
			if ($day_count == 1 || $day_count == 7) {
				$class = "class=\"eventweekend\"";
			}
			foreach ($this->Events as $key => $event) {
				$eventyear = intval(substr($event[0], 0, 4));
				$eventmonth = intval(substr($event[0], 4, 2));
				$eventday = intval(substr($event[0], 6, 2));
				$event_start = mktime(0, 0, 0, $eventmonth, $eventday, $eventyear);
				$event_end = mktime(23, 59, 59, $eventmonth, $eventday, $eventyear);
				// Check if we are working with an event period
				if (strlen($event[0]) == 16) {
					$eventyear_end = intval(substr($event[0], 8, 4));
					$eventmonth_end = intval(substr($event[0], 12, 2));
					$eventday_end = intval(substr($event[0], 14, 2));
					$event_end = mktime(23, 59, 59, $eventmonth_end, $eventday_end, $eventyear_end);
				}
				$event_now = mktime(0, 0, 0, $month, $day_num, $year);
				if ($event_now >= $event_start && $event_now <= $event_end) {
					if (($event_end - $event_start) >= (24 * 60 * 60))
						$class = "class=\"eventdayactiveperiod\"";
					else
						$class = "class=\"eventdayactive\"";
					if ($day_count == 1 || $day_count == 7) {
						if (($event_end - $event_start) >= (24 * 60 * 60))
							$class = "class=\"eventweekendactiveperiod\"";
						else
							$class = "class=\"eventweekendactive\"";
					}   
					$id = ($key + 13);
					break;
				}       
			}
			if ($yearnow == $year && $monthnow == $month && $daynow == $day_num) {
				$textstyle = 'style="color : #FF0000;font-weight : bold;"';
			} else {
				$textstyle='';
			}
			if ($id > 0) {
				echo "<td $class $textstyle onclick=\"expandCollapse('div$id')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\"> $day_num </td>"; 
			} else {
				echo "<td $class $textstyle> $day_num </td>"; 
			}
			$day_num++;
			$day_count++;
			if ($day_count > 7) {
				echo "</tr><tr>";
				$day_count = 1;
			}
		}
		$day_nextmonth = 1;
		if ($day_count == 1) {
			echo "<td colspan=\"7\">&nbsp;</td>";
		} else {
			while ($day_count > 1 && $day_count <= 7 ) {
				if ($day_count == 7) {
					if ($this->Hide)
						echo "<td>&nbsp;</td>";
					else
						echo "<td class=\"eventweekenddisabled\">$day_nextmonth</td>";
				} else {
					if ($this->Hide)
						echo "<td>&nbsp;</td>";
					else
						echo "<td class=\"eventdaydisabled\">$day_nextmonth</td>";
				}
				$day_count++;
				$day_nextmonth++;
			}
		}
		echo "</tr></table>\n";       
	}
        
}

?>
Return current item: EasyPhpEventCalendar Class