Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Php Calendar Script

Featured Replies

Hi All

 

Im trying to get this script to work, Icant seem to link the forward and backward navigation?

 

http://www.ben-smith.net/testing/calendar.php

 

Any help would be great.

 

Thanks Ben

 

<style type="text/css">
<!--
.calendarHeader { 
font-weight: bolder; 
color: #CC0000; 
background-color: #FFFFCC; 
}

.calendarToday { 
background-color: #FFFFFF;
}

.calendar { 
background-color: #FFFFCC;
}
-->
</style>





<?

// PHP Calendar Class Version 1.4 (5th March 2001)
//  
// Copyright David Wilkinson 2000 - 2001. All Rights reserved.
// 
// This software may be used, modified and distributed freely
// providing this copyright notice remains intact at the head 
// of the file.
//
// This software is freeware. The author accepts no liability for
// any loss or damages whatsoever incurred directly or indirectly 
// from the use of this script. The author of this software makes 
// no claims as to its fitness for any purpose whatsoever. If you 
// wish to use this software you should first satisfy yourself that 
// it meets your requirements.
//
// URL:   http://www.cascade.org.uk/software/php/calendar/
// Email: davidw@cascade.org.uk


class Calendar
{
/*
	Constructor for the Calendar class
*/
function Calendar()
{
}


/*
	Get the array of strings used to label the days of the week. This array contains seven 
	elements, one for each day of the week. The first entry in this array represents Sunday. 
*/
function getDayNames()
{
	return $this->dayNames;
}


/*
	Set the array of strings used to label the days of the week. This array must contain seven 
	elements, one for each day of the week. The first entry in this array represents Sunday. 
*/
function setDayNames($names)
{
	$this->dayNames = $names;
}

/*
	Get the array of strings used to label the months of the year. This array contains twelve 
	elements, one for each month of the year. The first entry in this array represents January. 
*/
function getMonthNames()
{
	return $this->monthNames;
}

/*
	Set the array of strings used to label the months of the year. This array must contain twelve 
	elements, one for each month of the year. The first entry in this array represents January. 
*/
function setMonthNames($names)
{
	$this->monthNames = $names;
}



/* 
	Gets the start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
  function getStartDay()
{
	return $this->startDay;
}

/* 
	Sets the start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
function setStartDay($day)
{
	$this->startDay = $day;
}


/* 
	Gets the start month of the year. This is the month that appears first in the year
	view. January = 1.
*/
function getStartMonth()
{
	return $this->startMonth;
}

/* 
	Sets the start month of the year. This is the month that appears first in the year
	view. January = 1.
*/
function setStartMonth($month)
{
	$this->startMonth = $month;
}


/*
	Return the URL to link to in order to display a calendar for a given month/year.
	You must override this method if you want to activate the "forward" and "back" 
	feature of the calendar.

	Note: If you return an empty string from this function, no navigation link will
	be displayed. This is the default behaviour.

	If the calendar is being displayed in "year" view, $month will be set to zero.
*/
function getCalendarLink($month, $year)
{
	return "";
}

/*
	Return the URL to link to  for a given date.
	You must override this method if you want to activate the date linking
	feature of the calendar.

	Note: If you return an empty string from this function, no navigation link will
	be displayed. This is the default behaviour.
*/
function getDateLink($day, $month, $year)
{
	return "";
}


/*
	Return the HTML for the current month
*/
function getCurrentMonthView()
{
	$d = getdate(time());
	return $this->getMonthView($d["mon"], $d["year"]);
}


/*
	Return the HTML for the current year
*/
function getCurrentYearView()
{
	$d = getdate(time());
	return $this->getYearView($d["year"]);
}


/*
	Return the HTML for a specified month
*/
function getMonthView($month, $year)
{
	return $this->getMonthHTML($month, $year);
}


/*
	Return the HTML for a specified year
*/
function getYearView($year)
{
	return $this->getYearHTML($year);
}



/********************************************************************************

	The rest are private methods. No user-servicable parts inside.

	You shouldn't need to call any of these functions directly.

***************************************************************************
******/


/*
	Calculate the number of days in a month, taking into account leap years.
*/
function getDaysInMonth($month, $year)
{
	if ($month < 1 || $month > 12)
	{
		return 0;
	}

	$d = $this->daysInMonth[$month - 1];

	if ($month == 2)
	{
		// Check for leap year
		// Forget the 4000 rule, I doubt I'll be around then...

		if ($year%4 == 0)
		{
			if ($year%100 == 0)
			{
				if ($year%400 == 0)
				{
					$d = 29;
				}
			}
			else
			{
				$d = 29;
			}
		}
	}

	return $d;
}


/*
	Generate the HTML for a given month
*/
function getMonthHTML($m, $y, $showYear = 1)
{
	$s = "";

	$a = $this->adjustDate($m, $y);
	$month = $a[0];
	$year = $a[1];		

	$daysInMonth = $this->getDaysInMonth($month, $year);
	$date = getdate(mktime(12, 0, 0, $month, 1, $year));

	$first = $date["wday"];
	$monthName = $this->monthNames[$month - 1];

	$prev = $this->adjustDate($month - 1, $year);
	$next = $this->adjustDate($month + 1, $year);

	if ($showYear == 1)
	{
		$prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
		$nextMonth = $this->getCalendarLink($next[0], $next[1]);
	}
	else
	{
		$prevMonth = "";
		$nextMonth = "";
	}

	$header = $monthName . (($showYear > 0) ? " " . $year : "");

	$s .= "<table class=\"calendar\">\n";
	$s .= "<tr>\n";
	$s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? " " : "<a href=\"$prevMonth\"><<</a>")  . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n"; 
	$s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? " " : "<a href=\"$nextMonth\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";

	$s .= "<tr>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
	$s .= "</tr>\n";

	// We need to work out what date to start at so that the first appears in the correct column
	$d = $this->startDay + 1 - $first;
	while ($d > 1)
	{
		$d -= 7;
	}

	// Make sure we know when today is, so that we can use a different CSS style
	$today = getdate(time());

	while ($d <= $daysInMonth)
	{
		$s .= "<tr>\n";	   

		for ($i = 0; $i < 7; $i++)
		{
			$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
			$s .= "<td class=\"$class\" align=\"right\" valign=\"top\">";	   
			if ($d > 0 && $d <= $daysInMonth)
			{
				$link = $this->getDateLink($d, $month, $year);
				$s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
			}
			else
			{
				$s .= " ";
			}
			  $s .= "</td>\n";	   
			$d++;
		}
		$s .= "</tr>\n";	
	}

	$s .= "</table>\n";

	return $s;	  
}


/*
	Generate the HTML for a given year
*/
function getYearHTML($year)
{
	$s = "";
	$prev = $this->getCalendarLink(0, $year - 1);
	$next = $this->getCalendarLink(0, $year + 1);

	$s .= "<table class=\"calendar\" border=\"0\">\n";
	$s .= "<tr>";
	$s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? " " : "<a href=\"$prev\"><<</a>")  . "</td>\n";
	$s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? " " : "<a href=\"$next\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "</table>\n";

	return $s;
}

/*
	Adjust dates to allow months > 12 and < 0. Just adjust the years appropriately.
	e.g. Month 14 of the year 2001 is actually month 2 of year 2002.
*/
function adjustDate($month, $year)
{
	$a = array();  
	$a[0] = $month;
	$a[1] = $year;

	while ($a[0] > 12)
	{
		$a[0] -= 12;
		$a[1]++;
	}

	while ($a[0] <= 0)
	{
		$a[0] += 12;
		$a[1]--;
	}

	return $a;
}

/* 
	The start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
var $startDay = 0;

/* 
	The start month of the year. This is the month that appears in the first slot
	of the calendar in the year view. January = 1.
*/
var $startMonth = 1;

/*
	The labels to display for the days of the week. The first entry in this array
	represents Sunday.
*/
var $dayNames = array("S", "M", "T", "W", "T", "F", "S");

/*
	The labels to display for the months of the year. The first entry in this array
	represents January.
*/
var $monthNames = array("January", "February", "March", "April", "May", "June",
						"July", "August", "September", "October", "November", "December");


/*
	The number of days in each month. You're unlikely to want to change this...
	The first entry in this array represents January.
*/
var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

}

?>

<?

class MyCalendar extends Calendar
{
function getCalendarLink($month, $year)
{
	// Redisplay the current page, but with some parameters
	// to set the new month and year
	$s = getenv('SCRIPT_NAME');
	return "$s?month=$month&year=$year";
}
}

// If no month/year set, use current month/year

$d = getdate(time());

if ($month == "")
{
$month = $d["mon"];
}

if ($year == "")
{
$year = $d["year"];
}

$cal = new MyCalendar;
echo $cal->getMonthView($month, $year);
?>

Funny that the example on the website you got it from does not work either...

 

I will take a look though

I tried this and it works perfectly

 

Put this after the calendar class:

 

$d = getdate(time());

$month = $d['mon'];
$year = $d['year'];

if(isset($_GET['month'])) {
if( (is_numeric($_GET['month'])) && ($_GET['month'] <= 12) && ($_GET['month'] >= 1) ) {
	$month = $_GET['month'];
}
}

if(isset($_GET['year'])) {
if( (is_numeric($_GET['year'])) && ($_GET['year'] <= 9999) && ($_GET['year'] >= 1970) ) {
	$year = $_GET['year'];
}
}

$cal = new MyCalendar;

echo $cal->getMonthView($month, $year);

 

Works here: http://www.ajrdev.com/cal.php

Hi All

 

Im trying to get this script to work, Icant seem to link the forward and backward navigation?

 

http://www.ben-smith.net/testing/calendar.php

 

Any help would be great.

 

Thanks Ben

 

<style type="text/css">
<!--
.calendarHeader { 
font-weight: bolder; 
color: #CC0000; 
background-color: #FFFFCC; 
}

.calendarToday { 
background-color: #FFFFFF;
}

.calendar { 
background-color: #FFFFCC;
}
-->
</style>





<?

// PHP Calendar Class Version 1.4 (5th March 2001)
//  
// Copyright David Wilkinson 2000 - 2001. All Rights reserved.
// 
// This software may be used, modified and distributed freely
// providing this copyright notice remains intact at the head 
// of the file.
//
// This software is freeware. The author accepts no liability for
// any loss or damages whatsoever incurred directly or indirectly 
// from the use of this script. The author of this software makes 
// no claims as to its fitness for any purpose whatsoever. If you 
// wish to use this software you should first satisfy yourself that 
// it meets your requirements.
//
// URL:   http://www.cascade.org.uk/software/php/calendar/
// Email: davidw@cascade.org.uk


class Calendar
{
/*
	Constructor for the Calendar class
*/
function Calendar()
{
}


/*
	Get the array of strings used to label the days of the week. This array contains seven 
	elements, one for each day of the week. The first entry in this array represents Sunday. 
*/
function getDayNames()
{
	return $this->dayNames;
}


/*
	Set the array of strings used to label the days of the week. This array must contain seven 
	elements, one for each day of the week. The first entry in this array represents Sunday. 
*/
function setDayNames($names)
{
	$this->dayNames = $names;
}

/*
	Get the array of strings used to label the months of the year. This array contains twelve 
	elements, one for each month of the year. The first entry in this array represents January. 
*/
function getMonthNames()
{
	return $this->monthNames;
}

/*
	Set the array of strings used to label the months of the year. This array must contain twelve 
	elements, one for each month of the year. The first entry in this array represents January. 
*/
function setMonthNames($names)
{
	$this->monthNames = $names;
}



/* 
	Gets the start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
  function getStartDay()
{
	return $this->startDay;
}

/* 
	Sets the start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
function setStartDay($day)
{
	$this->startDay = $day;
}


/* 
	Gets the start month of the year. This is the month that appears first in the year
	view. January = 1.
*/
function getStartMonth()
{
	return $this->startMonth;
}

/* 
	Sets the start month of the year. This is the month that appears first in the year
	view. January = 1.
*/
function setStartMonth($month)
{
	$this->startMonth = $month;
}


/*
	Return the URL to link to in order to display a calendar for a given month/year.
	You must override this method if you want to activate the "forward" and "back" 
	feature of the calendar.

	Note: If you return an empty string from this function, no navigation link will
	be displayed. This is the default behaviour.

	If the calendar is being displayed in "year" view, $month will be set to zero.
*/
function getCalendarLink($month, $year)
{
	return "";
}

/*
	Return the URL to link to  for a given date.
	You must override this method if you want to activate the date linking
	feature of the calendar.

	Note: If you return an empty string from this function, no navigation link will
	be displayed. This is the default behaviour.
*/
function getDateLink($day, $month, $year)
{
	return "";
}


/*
	Return the HTML for the current month
*/
function getCurrentMonthView()
{
	$d = getdate(time());
	return $this->getMonthView($d["mon"], $d["year"]);
}


/*
	Return the HTML for the current year
*/
function getCurrentYearView()
{
	$d = getdate(time());
	return $this->getYearView($d["year"]);
}


/*
	Return the HTML for a specified month
*/
function getMonthView($month, $year)
{
	return $this->getMonthHTML($month, $year);
}


/*
	Return the HTML for a specified year
*/
function getYearView($year)
{
	return $this->getYearHTML($year);
}



/********************************************************************************

	The rest are private methods. No user-servicable parts inside.

	You shouldn't need to call any of these functions directly.

***************************************************************************
******/


/*
	Calculate the number of days in a month, taking into account leap years.
*/
function getDaysInMonth($month, $year)
{
	if ($month < 1 || $month > 12)
	{
		return 0;
	}

	$d = $this->daysInMonth[$month - 1];

	if ($month == 2)
	{
		// Check for leap year
		// Forget the 4000 rule, I doubt I'll be around then...

		if ($year%4 == 0)
		{
			if ($year%100 == 0)
			{
				if ($year%400 == 0)
				{
					$d = 29;
				}
			}
			else
			{
				$d = 29;
			}
		}
	}

	return $d;
}


/*
	Generate the HTML for a given month
*/
function getMonthHTML($m, $y, $showYear = 1)
{
	$s = "";

	$a = $this->adjustDate($m, $y);
	$month = $a[0];
	$year = $a[1];		

	$daysInMonth = $this->getDaysInMonth($month, $year);
	$date = getdate(mktime(12, 0, 0, $month, 1, $year));

	$first = $date["wday"];
	$monthName = $this->monthNames[$month - 1];

	$prev = $this->adjustDate($month - 1, $year);
	$next = $this->adjustDate($month + 1, $year);

	if ($showYear == 1)
	{
		$prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
		$nextMonth = $this->getCalendarLink($next[0], $next[1]);
	}
	else
	{
		$prevMonth = "";
		$nextMonth = "";
	}

	$header = $monthName . (($showYear > 0) ? " " . $year : "");

	$s .= "<table class=\"calendar\">\n";
	$s .= "<tr>\n";
	$s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? " " : "<a href=\"$prevMonth\"><<</a>")  . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n"; 
	$s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? " " : "<a href=\"$nextMonth\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";

	$s .= "<tr>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
	$s .= "</tr>\n";

	// We need to work out what date to start at so that the first appears in the correct column
	$d = $this->startDay + 1 - $first;
	while ($d > 1)
	{
		$d -= 7;
	}

	// Make sure we know when today is, so that we can use a different CSS style
	$today = getdate(time());

	while ($d <= $daysInMonth)
	{
		$s .= "<tr>\n";	   

		for ($i = 0; $i < 7; $i++)
		{
			$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
			$s .= "<td class=\"$class\" align=\"right\" valign=\"top\">";	   
			if ($d > 0 && $d <= $daysInMonth)
			{
				$link = $this->getDateLink($d, $month, $year);
				$s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
			}
			else
			{
				$s .= " ";
			}
			  $s .= "</td>\n";	   
			$d++;
		}
		$s .= "</tr>\n";	
	}

	$s .= "</table>\n";

	return $s;	  
}


/*
	Generate the HTML for a given year
*/
function getYearHTML($year)
{
	$s = "";
	$prev = $this->getCalendarLink(0, $year - 1);
	$next = $this->getCalendarLink(0, $year + 1);

	$s .= "<table class=\"calendar\" border=\"0\">\n";
	$s .= "<tr>";
	$s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? " " : "<a href=\"$prev\"><<</a>")  . "</td>\n";
	$s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? " " : "<a href=\"$next\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "</table>\n";

	return $s;
}

/*
	Adjust dates to allow months > 12 and < 0. Just adjust the years appropriately.
	e.g. Month 14 of the year 2001 is actually month 2 of year 2002.
*/
function adjustDate($month, $year)
{
	$a = array();  
	$a[0] = $month;
	$a[1] = $year;

	while ($a[0] > 12)
	{
		$a[0] -= 12;
		$a[1]++;
	}

	while ($a[0] <= 0)
	{
		$a[0] += 12;
		$a[1]--;
	}

	return $a;
}

/* 
	The start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
var $startDay = 0;

/* 
	The start month of the year. This is the month that appears in the first slot
	of the calendar in the year view. January = 1.
*/
var $startMonth = 1;

/*
	The labels to display for the days of the week. The first entry in this array
	represents Sunday.
*/
var $dayNames = array("S", "M", "T", "W", "T", "F", "S");

/*
	The labels to display for the months of the year. The first entry in this array
	represents January.
*/
var $monthNames = array("January", "February", "March", "April", "May", "June",
						"July", "August", "September", "October", "November", "December");


/*
	The number of days in each month. You're unlikely to want to change this...
	The first entry in this array represents January.
*/
var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

}

?>

<?

class MyCalendar extends Calendar
{
function getCalendarLink($month, $year)
{
	// Redisplay the current page, but with some parameters
	// to set the new month and year
	$s = getenv('SCRIPT_NAME');
	return "$s?month=$month&year=$year";
}
}

// If no month/year set, use current month/year

$d = getdate(time());

if ($month == "")
{
$month = $d["mon"];
}

if ($year == "")
{
$year = $d["year"];
}

$cal = new MyCalendar;
echo $cal->getMonthView($month, $year);
?>

 

 

Try following ( By setting the passed varibles using $_get() ;

 

// If no month/year set, use current month/year

 

$d = getdate(time());

 

if (isset($_GET["month"])) {$month = $_GET["month"]; }

 

if (isset($_GET["year"])) {$year = $_GET["year"]; }

 

 

if ($month == "")

{

$month = $d["mon"];

}

 

if ($year == "")

{

$year = $d["year"];

}

 

$cal = new MyCalendar;

echo $cal->getMonthView($month, $year);

?>

Yea that would work, essentially the same as my example except mine includes a bit more validation to stop it breaking if the $_GET variables have invalid values

Yea that would work, essentially the same as my example except mine includes a bit more validation to stop it breaking if the $_GET variables have invalid values

 

 

I left the validation out because this is not a user input from a form. The previous month and next month are calculated within the class there fore I think the validation is redundant in this case, just to make the solution simple and understandable I just stated the neccessary code.

  • Author

Thanks guys that great! :)

 

<style type="text/css">
<!--
.calendarHeader { 
font-weight: bolder; 
color: #CC0000; 
background-color: #FFFFCC; 
}

.calendarToday { 
background-color: #FFFFFF;
}

.calendarBooked{ 
background-color: red;
}

.calendarAvailable{ 
background-color: green;
}


.calendar { 
background-color: #FFFFCC;
}
-->
</style>





<?

// PHP Calendar Class Version 1.4 (5th March 2001)
//  
// Copyright David Wilkinson 2000 - 2001. All Rights reserved.
// 
// This software may be used, modified and distributed freely
// providing this copyright notice remains intact at the head 
// of the file.
//
// This software is freeware. The author accepts no liability for
// any loss or damages whatsoever incurred directly or indirectly 
// from the use of this script. The author of this software makes 
// no claims as to its fitness for any purpose whatsoever. If you 
// wish to use this software you should first satisfy yourself that 
// it meets your requirements.
//
// URL:   http://www.cascade.org.uk/software/php/calendar/
// Email: davidw@cascade.org.uk


class Calendar
{
/*
	Constructor for the Calendar class
*/
function Calendar()
{
}


/*
	Get the array of strings used to label the days of the week. This array contains seven 
	elements, one for each day of the week. The first entry in this array represents Sunday. 
*/
function getDayNames()
{
	return $this->dayNames;
}


/*
	Set the array of strings used to label the days of the week. This array must contain seven 
	elements, one for each day of the week. The first entry in this array represents Sunday. 
*/
function setDayNames($names)
{
	$this->dayNames = $names;
}

/*
	Get the array of strings used to label the months of the year. This array contains twelve 
	elements, one for each month of the year. The first entry in this array represents January. 
*/
function getMonthNames()
{
	return $this->monthNames;
}

/*
	Set the array of strings used to label the months of the year. This array must contain twelve 
	elements, one for each month of the year. The first entry in this array represents January. 
*/
function setMonthNames($names)
{
	$this->monthNames = $names;
}



/* 
	Gets the start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
  function getStartDay()
{
	return $this->startDay;
}

/* 
	Sets the start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
function setStartDay($day)
{
	$this->startDay = $day;
}


/* 
	Gets the start month of the year. This is the month that appears first in the year
	view. January = 1.
*/
function getStartMonth()
{
	return $this->startMonth;
}

/* 
	Sets the start month of the year. This is the month that appears first in the year
	view. January = 1.
*/
function setStartMonth($month)
{
	$this->startMonth = $month;
}


/*
	Return the URL to link to in order to display a calendar for a given month/year.
	You must override this method if you want to activate the "forward" and "back" 
	feature of the calendar.

	Note: If you return an empty string from this function, no navigation link will
	be displayed. This is the default behaviour.

	If the calendar is being displayed in "year" view, $month will be set to zero.
*/
function getCalendarLink($month, $year)
{
	return "";
}

/*
	Return the URL to link to  for a given date.
	You must override this method if you want to activate the date linking
	feature of the calendar.

	Note: If you return an empty string from this function, no navigation link will
	be displayed. This is the default behaviour.
*/
function getDateLink($day, $month, $year)
{
	return "";
}


/*
	Return the HTML for the current month
*/
function getCurrentMonthView()
{
	$d = getdate(time());
	return $this->getMonthView($d["mon"], $d["year"]);
}


/*
	Return the HTML for the current year
*/
function getCurrentYearView()
{
	$d = getdate(time());
	return $this->getYearView($d["year"]);
}


/*
	Return the HTML for a specified month
*/
function getMonthView($month, $year)
{
	return $this->getMonthHTML($month, $year);
}


/*
	Return the HTML for a specified year
*/
function getYearView($year)
{
	return $this->getYearHTML($year);
}



/********************************************************************************

	The rest are private methods. No user-servicable parts inside.

	You shouldn't need to call any of these functions directly.

***************************************************************************
******/


/*
	Calculate the number of days in a month, taking into account leap years.
*/
function getDaysInMonth($month, $year)
{
	if ($month < 1 || $month > 12)
	{
		return 0;
	}

	$d = $this->daysInMonth[$month - 1];

	if ($month == 2)
	{
		// Check for leap year
		// Forget the 4000 rule, I doubt I'll be around then...

		if ($year%4 == 0)
		{
			if ($year%100 == 0)
			{
				if ($year%400 == 0)
				{
					$d = 29;
				}
			}
			else
			{
				$d = 29;
			}
		}
	}

	return $d;
}


/*
	Generate the HTML for a given month
*/
function getMonthHTML($m, $y, $showYear = 1)
{
	$s = "";

	$a = $this->adjustDate($m, $y);
	$month = $a[0];
	$year = $a[1];		

	$daysInMonth = $this->getDaysInMonth($month, $year);
	$date = getdate(mktime(12, 0, 0, $month, 1, $year));

	$first = $date["wday"];
	$monthName = $this->monthNames[$month - 1];

	$prev = $this->adjustDate($month - 1, $year);
	$next = $this->adjustDate($month + 1, $year);

	if ($showYear == 1)
	{
		$prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
		$nextMonth = $this->getCalendarLink($next[0], $next[1]);
	}
	else
	{
		$prevMonth = "";
		$nextMonth = "";
	}

	$header = $monthName . (($showYear > 0) ? " " . $year : "");

	$s .= "<table class=\"calendar\">\n";
	$s .= "<tr>\n";
	$s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? " " : "<a href=\"$prevMonth\"><<</a>")  . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n"; 
	$s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? " " : "<a href=\"$nextMonth\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";

	$s .= "<tr>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
	$s .= "</tr>\n";

	// We need to work out what date to start at so that the first appears in the correct column
	$d = $this->startDay + 1 - $first;
	while ($d > 1)
	{
		$d -= 7;
	}

	// Make sure we know when today is, so that we can use a different CSS style
	$today = getdate(time());

	while ($d <= $daysInMonth)
	{
		$s .= "<tr>\n";	   

		for ($i = 0; $i < 7; $i++)
		{
			$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
			$s .= "<td class=\"$class\" align=\"right\" valign=\"top\">";	   
			if ($d > 0 && $d <= $daysInMonth)
			{
				$link = $this->getDateLink($d, $month, $year);
				$s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
			}
			else
			{
				$s .= " ";
			}
			  $s .= "</td>\n";	   
			$d++;
		}
		$s .= "</tr>\n";	
	}

	$s .= "</table>\n";

	return $s;	  
}


/*
	Generate the HTML for a given year
*/
function getYearHTML($year)
{
	$s = "";
	$prev = $this->getCalendarLink(0, $year - 1);
	$next = $this->getCalendarLink(0, $year + 1);

	$s .= "<table class=\"calendar\" border=\"0\">\n";
	$s .= "<tr>";
	$s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? " " : "<a href=\"$prev\"><<</a>")  . "</td>\n";
	$s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? " " : "<a href=\"$next\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "</table>\n";

	return $s;
}

/*
	Adjust dates to allow months > 12 and < 0. Just adjust the years appropriately.
	e.g. Month 14 of the year 2001 is actually month 2 of year 2002.
*/
function adjustDate($month, $year)
{
	$a = array();  
	$a[0] = $month;
	$a[1] = $year;

	while ($a[0] > 12)
	{
		$a[0] -= 12;
		$a[1]++;
	}

	while ($a[0] <= 0)
	{
		$a[0] += 12;
		$a[1]--;
	}

	return $a;
}

/* 
	The start day of the week. This is the day that appears in the first column
	of the calendar. Sunday = 0.
*/
var $startDay = 0;

/* 
	The start month of the year. This is the month that appears in the first slot
	of the calendar in the year view. January = 1.
*/
var $startMonth = 1;

/*
	The labels to display for the days of the week. The first entry in this array
	represents Sunday.
*/
var $dayNames = array("S", "M", "T", "W", "T", "F", "S");

/*
	The labels to display for the months of the year. The first entry in this array
	represents January.
*/
var $monthNames = array("January", "February", "March", "April", "May", "June",
						"July", "August", "September", "October", "November", "December");


/*
	The number of days in each month. You're unlikely to want to change this...
	The first entry in this array represents January.
*/
var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

}

?>

<?

$d = getdate(time());

$month = $d['mon'];
$year = $d['year'];

if(isset($_GET['month'])) {
if( (is_numeric($_GET['month'])) && ($_GET['month'] <= 12) && ($_GET['month'] >= 1) ) {
	$month = $_GET['month'];
}
}

if(isset($_GET['year'])) {
if( (is_numeric($_GET['year'])) && ($_GET['year'] <= 9999) && ($_GET['year'] >= 1970) ) {
	$year = $_GET['year'];
}
}

$cal = new MyCalendar;

echo $cal->getMonthView($month, $year);

class MyCalendar extends Calendar
{
function getCalendarLink($month, $year)
{
	// Redisplay the current page, but with some parameters
	// to set the new month and year
	$s = getenv('SCRIPT_NAME');
	return "$s?month=$month&year=$year";
}
}


?>

 

Next stage im going to be working on is setting the background of each date depending if the date is available or not.

 

Ive never tried this with functions before and getting a bit lost.

 

In the past Ive done something like this:

 

 

if(($myrow['type'] == 'Confirmed')){
		  echo '<td cellpadding="0"><span class="calendarBooked">' .$myrow['type'].'</span></td>';

 } else {
  echo'<td cellpadding="0"><span class="calendarAvailable">' .$myrow['type'].'</span></td>';
 }

 

 

but not sure where to implement it in here?

 

function getYearHTML($year)
{
	$s = "";
	$prev = $this->getCalendarLink(0, $year - 1);
	$next = $this->getCalendarLink(0, $year + 1);

	$s .= "<table class=\"calendar\" border=\"0\">\n";
	$s .= "<tr>";
	$s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? " " : "<a href=\"$prev\"><<</a>")  . "</td>\n";
	$s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
	$s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? " " : "<a href=\"$next\">>></a>")  . "</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "<tr>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
	$s .= "</tr>\n";
	$s .= "</table>\n";

	return $s;
}

 

 

 

Thanks B

I left the validation out because this is not a user input from a form. The previous month and next month are calculated within the class there fore I think the validation is redundant in this case, just to make the solution simple and understandable I just stated the neccessary code. But I think If your are going to extend this program to allow user input then you would implemnt something like what you have sated
I left the validation out because this is not a user input from a form.

theres nothing wrong with validating all input however... and $_GET[''] is still user input... just pointing it out as ALL input should ALLWAYS be validated...

 

one thing i do is validate ALL input and save it to a new array replacing $_POST/$_GET with $clean['array_key'], im sure skysurge, Jem and other coders/x-hackers will agree that all it takes is one code screw up to cripple an entire system... its a coding mentality thing really... you don't want to get in a habit of saying ahh that dosnt need validating because X, Y & Z..... because what if Y fails... what's gonna stop unwanted activity on your system?

 

maybe im just over paranoid... but then its not paranoia if its true....

for instance what if an attacker trys to abuse your lack of validation with an attacking loading this up

http://www.webviews.co.uk/weather/id.txt

that is from a real script attack attempted today on one of my servers, and is still being used by the attacker on other sites... obviously it dint go through, my system just reported it to me and applied a automatic network ban...

 

that was an attack using the $_GET route into the application... so yeah don't trust any input unless your system created it

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.