<?php

class calendar{

	var $thedate;
	var $startday;
	var $tabletags;
	var $tdinmonth;
	var $tdoutmonth;
	var $tdtoday;
	var $tdtodayevent;
	var $tdevent;
	var $tdoutevent;
	var $dayformat;
	var $thtags;
	var $themonth;
	var $theday;
	var $theyear;
	var $firstofmonthts;

	function setdate($thedate){
		$bits = split("-", $thedate);
		$this->thedate = $thedate;
		$this->themonth = (int)$bits[1];
		$this->theyear = $bits[0];
		$this->theday = (int)$bits[2];
		$this->firstofmonthts = mktime(0,0,0,$this->themonth, 1, $this->theyear);
	}

	function dayofweek(){
		return date("w", strtotime($this->thedate));
	}

	function notaweekend($frimon = -1){
		$day = $this->dayofweek();
		if( $day == 0 ){	// sunday
			$this->setdate( $this->dateadd( "%Y-%m-%d", $frimon == -1 ? -2 : 1));
		}elseif( $day == 6 ){	// saturday
			$this->setdate($this->dateadd("%Y-%m-%d", $frimon == -1 ? -1 : 2));
		}
	}

	function addweekdays($format = "%Y-%m-%d", $days=0){
		$inc = $days < 0 ? -1 : 1;
		$days = abs($days);
		$firstday = $this->dayofweek();
		$numdays = 0;
		$day = 0;
		while( $day < $days ){
			if( $firstday != 0 && $firstday != 6 ) $day++;
			$numdays++;
			$firstday += $inc;
			if( $firstday < 0 ) $firstday = 6;
			if( $firstday > 6 ) $firstday = 0;
		}
		return strftime($format, mktime(0,0,0,$this->themonth, $this->theday+($numdays * $inc), $this->theyear));
	}

	function dateadd($format = "%Y-%m-%d", $days = 0, $months = 0, $years = 0, $keepday = true){
		if( $days == 0 && $keepday == true ){
			$daysinnewmonth = date("t", mktime(0,0,0,$this->themonth+$months, $this->theyear+$years));
			if( $daysinnewmonth < $this->theday ){
				return strftime($format, mktime(0,0,0,$this->themonth+$months, $daysinnewmonth, $this->theyear+$years));
			}else{
				return strftime($format, mktime(0,0,0,$this->themonth+$months, $this->theday+$days, $this->theyear+$years));
			}
		}else{
			return strftime($format, mktime(0,0,0,$this->themonth+$months, $this->theday+$days, $this->theyear+$years));
		}
	}

	function getdate($format = "%Y-%m-%d"){return strftime($format, mktime(0,0,0,$this->themonth, $this->theday, $this->theyear));}
	function nextmonth($format = "%Y-%m-%d"){return $this->dateadd($format, 0, 1, 0);}
	function prevmonth($format = "%Y-%m-%d"){return $this->dateadd($format, 0, -1, 0);}
	function nextday($format = "%Y-%m-%d"){return $this->dateadd($format, 1, 0, 0, false);}
	function prevday($format = "%Y-%m-%d"){return $this->dateadd($format, -1, 0,0, false);}
	function nextyear($format = "%Y-%m-%d"){return $this->dateadd($format, 0, 0, 1);}
	function prevyear($format = "%Y-%m-%d"){return $this->dateadd($format, 0, 0, -1);}
	function nextweek($format = "%Y-%m-%d"){return $this->dateadd($format, 7, 0, 0, false);}
	function prevweek($format = "%Y-%m-%d"){return $this->dateadd($format, -7, 0,0, false);}

	function daysinmonth(){
		return (int)date("t", mktime(0,0,0,$this->themonth, 1, $this->theyear));
	}

	function calendar($thedate = "", $startday = 0){
		global $HTTP_SERVER_VARS;
		if( $thedate == "" ) $thedate = date("Y-m-d");
		$this->setdate($thedate);
		$this->startday = $startday;
		$this->tabletags = "<table>";
		$this->tdinmonth = "<td width='32' height='32'><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a></td>\n";
		$this->tdoutmonth = "<td>%day%</td>\n";
		$this->tdinmonthevent = "<td><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a><br>%event%</td>\n";
		$this->tdoutmonthevent = "<td>%day%<br>%event%</td>\n";
		$this->dayformat = "full";
		$this->thtags = "<th width='32'>%day%</th>";
		$this->tdtoday = "<td bgcolor='#bbbbbb'><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a></td>\n";
		$this->tdtodayevent = "<td bgcolor='#bbbbbb'><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a><br>%event%</td>\n";
	}

	function getcalendardays(){
		$daysinmonth = $this->daysinmonth();
		$firstdayofmonth = date("w", $this->firstofmonthts);
		$lastdayofmonth = date("w", mktime(0,0,0,$this->themonth, $daysinmonth, $this->theyear));
		$daysinlastmonth = date("t", mktime(0,0,0,$this->themonth-1, 1, $this->theyear));
		$daysback = $firstdayofmonth - $this->startday;
		if( $daysback < 0 ){
			$daysback += 7;
		}
		$days = array();
		for( $i = $daysback-1; $i >= 0; $i--) $days[] = $daysinlastmonth - $i;
		for( $i = 1; $i <= $daysinmonth; $i++){
			$days[] = $i;
		}
		if( count($days) % 7 != 0 ){
			$daysremaining = 7 - (count($days) % 7);
			for( $i = 1; $i <= $daysremaining; $i++) $days[] = $i;
		}
		return $days;
	}

	function getdaynames(&$days, $themonth, $theyear){
		$daynames = array();
		switch ($this->dayformat){
			case "full":
				$dformat = "%A";
				break;
			default:
				$dformat = "%a";
				break;
		}
		for( $i = 0; $i < 7; $i++){
			if( $days[$i] > 7 ){
				$daynames[$i] = strftime( $dformat, mktime(0,0,0,$themonth-1, $days[$i], $theyear));
			}else{
				$daynames[$i] = strftime( $dformat, mktime(0,0,0,$themonth, $days[$i], $theyear));
			}
			if( $this->dayformat == "short" ) $daynames[$i] = substr($daynames[$i], 0, 1);
		}
		return $daynames;
	}

	function displaycalendar($events = array()){
		$thedays = $this->getcalendardays();
		$dayofweek = 0;
		$inmonth = 0;
		$daynames = $this->getdaynames($thedays, $this->themonth, $this->theyear);
		echo "<table border=1 cellpadding=0 cellspacing=0 align=center>\n";
		echo "<tr>\n";
		for( $i = 0; $i < 7; $i++)	echo str_replace("%day%", $daynames[$i], $this->thtags);
		echo "</tr>\n";
		$search = array("%day%", "%date%", "%event%");
		for( $i = 0; $i < count($thedays); $i++){
			$event = "";
			if( $dayofweek == 0 ){
				echo "<tr>\n";
			}elseif( $dayofweek == 7 ){
				echo "</tr>\n";
				$dayofweek = 0;
			}
			if( $thedays[$i] == 1 ){
				$inmonth = 1 - $inmonth;
			}
			if( $inmonth ){
				$thedate = date("Y-m-d", mktime( 0,0,0,$this->themonth, $thedays[$i], $this->theyear));
				if( $thedays[$i] == $this->theday ){
					if( isset($events["$thedate"]) ){
						$out = $this->tdtodayevent;
						$event = $events["$thedate"];
					}else{
						$out = $this->tdtoday;
					}
				}else{
					if( isset($events["$thedate"]) ){
						$out = $this->tdinmonthevent;
						$event = $events["$thedate"];
					}else{
						$out = $this->tdinmonth;
					}
				}
			}else{
				if( $thedays[$i] < 7 ){
					$thedate = date("Y-m-d", mktime(0,0,0,$this->themonth+1, $thedays[$i], $this->theyear));
				}else{
					$thedate = date("Y-m-d", mktime(0,0,0,$this->themonth-1, $thedays[$i], $this->theyear));
				}
				if( isset($events["$thedate"]) ){
					$out = $this->tdoutmonthevent;
					$event = $events["$thedate"];
				}else{
					$out = $this->tdoutmonth;
				}
			}
			echo str_replace($search,array($thedays[$i], $thedate, $event), $out);
			$dayofweek++;
		}
		if( $dayofweek == 7 ) echo "</tr>\n";
		echo "</table>\n";
	}

}

?>