Linking Calendar Dates: Advanced Example

This example is based around a WebCam Archive I created some time ago . The reason I originally wrote the PHP Calendar class was because I wanted a way of easily navigating through the WebCam Archive. The problem here is that not all days have images stored against them. I wanted it to be obvious from the calendar which days had images and which days didn't.

The WebCam images are stored in a set of directories. Each directory has a name of the form yyyymmdd. For example, images for the 3rd February 2001 would be stored in a directory called 20010203. A simple way to determine whether any images exist for a give day is to check whether a directory of the appropriate name exists - if it does, then we have some images.

The link returned by the getDateLink function also will vary depending on the date chosen. In this case, it simply tags some parameters onto the end of the current url.

$webcamArchive = "/path/to/webcam/archive"; // This function pads a number to a fixed length by // adding leading zeroes. // e.g. "3" padded to length "2" returns "03" function pad($s, $n) { $r = $s; while (strlen($r) < $n) { $r = "0".$r; } return $r; } // this function takes a day/month/year and returns // the directory name that images for the day would be // stored in. function getDirName($day, $month, $year) { return pad($year, 4) . pad($month, 2) . pad($day, 2); } // Now define the Calendar class class WebCamCalendar extends Calendar { function getCalendarLink($month, $year) { $s = getenv('SCRIPT_NAME'); return "$s?month=$month&year=$year"; } function getDateLink($day, $month, $year) { global $webcamArchive; $dir = $webcamArchive . "/" . getDirName($day, $month, $year); $s = getenv('SCRIPT_NAME'); if (file_exists($dir)) { return "$s?day=$day&month=$month&year=$year"; } else { return ""; } } }

Back to the Calendar page