<?PHP
	session_start();

	define(SECSADAY, 24*60*60);
	define(CYEAR, date('Y'));
	define(CMONTH, date('n'));

	if(strlen($_GET[viewDate]) == 7)
	{
		// set a session variable to keep track of what month we are looking at
		// Perform a check to see if this is an actual date
		$_SESSION[showDate] = $_GET[viewDate];
	}

	if($_SESSION[showDate] == "")
		$_SESSION[showDate] = date('Y-m');

	function getTimeStamp($src, $dst)
	{
		$sourceDate = explode('-', date('Y-m', $src));
		$targetDate = explode('-', $dst);

		if($sourceDate[0] < $targetDate[0])
		{
			if(date('L', $src))
				return getTimeStamp($src + 366 * SECSADAY, $dst);
			else
				return getTimeStamp($src + 365 * SECSADAY, $dst);
		}
		elseif($sourceDate[0] > $targetDate[0])
		{
			if(date('L', $src))
				return getTimeStamp($src - 366 * SECSADAY, $dst);
			else
				return getTimeStamp($src - 365 * SECSADAY, $dst);
		}
		else	// we are on the correct year
		{
			if($sourceDate[1] < $targetDate[1])
				return getTimeStamp($src + date('t', $src) * SECSADAY, $dst);
			elseif($sourceDate[1] > $targetDate[1])
				return getTimeStamp($src + date('t', $src) * SECSADAY, $dst);
			else	// we are on the correct month
				return $src - (date('d', $src) - 2) * SECSADAY;
		}
		return "Something went wrong";
	}

	function printDay($timestamp)
	{
		// check if this is a sat or sun, print red bg if so
//		echo "<td><a href=\"$_SERVER[PHP_SELF]?viewDate=" . date('Y-m-d', $timestamp) . "\" class=\"calendarlink\">" . date('d', $timestamp) . "</a></td>\n";
		echo "<td><a href=\"calendar.php?viewDate=" . date('Y-m-d', $timestamp) . "\" class=\"calendarlink\">" . date('d', $timestamp) . "</a></td>\n";
	}

	function showCalendar()
	{
		$targetTime = getTimeStamp(time(), $_SESSION[showDate]);
		$thisMonth = date('F', $targetTime);
		$nextMonth = date('Y-m', $targetTime + (date('t', $targetTime)) * SECSADAY);
		$prevMonth = date('Y-m', $targetTime - SECSADAY * 3);
		echo "<table border=0 cellpadding=1 cellspacing=0 class=\"calendar\"><tr>\n";
		echo "<td><a href=\"$_SERVER[PHP_SELF]?viewDate=$prevMonth\" class=\"calendarlink\">&lt;</a></td>\n";
		echo "<td colspan=5><center>$thisMonth</center></td>\n";
		echo "<td><a href=\"$_SERVER[PHP_SELF]?viewDate=$nextMonth\" class=\"calendarlink\">&gt;</a></td>\n</tr>\n";

		// print, week by week, the last week of previous month, this month and the first week of the next month

		$firstDayStamp = $targetTime - (date('j', $targetTime) -1) * SECSADAY;
		$lastDayStamp = $firstDayStamp + (date('t', $firstDayStamp) - 1) * SECSADAY;
		$lastDay = date('w', $lastDayStamp);
		if($lastDay == 0)
			$lastDay = 7;
		$lastDay--;
		if($lastDay == 0) // or if this is the current year, month and todays date is above the 15th
			$targetStamp = $lastDayStamp + 7 * SECSADAY;
		else
			$targetStamp = $lastDayStamp + (6 - $lastDay) * SECSADAY;


		$firstDay = date('w', $firstDayStamp);
		if($firstDay == 0)
			$firstDay = 7;
		$firstDay--;
		if($firstDay == 0) // or if this is the current year, month and todays date is below the tenth
			$startStamp = $firstDayStamp - 7 * SECSADAY;
		else
			$startStamp = $firstDayStamp - $firstDay * SECSADAY;

		echo "<tr>\n";
		for(;$startStamp <= $targetStamp; $startStamp += SECSADAY)
		{
			printDay($startStamp);
			if(date('w', $startStamp) == 0)
				echo "</tr>\n<tr>\n";
		}

		echo "</table>\n";
	}

	echo '<link rel="stylesheet" type="text/css" href="calendar.css">';

	showCalendar();

PHP?>
