diff --git a/cal.php b/cal.php
index cc8c029e4..3c944cc4f 100644
--- a/cal.php
+++ b/cal.php
@@ -17,6 +17,13 @@ $cy = isset($cy) ? (int) $cy : 0;
$cm = isset($cm) ? (int) $cm : 0;
$cd = isset($cd) ? (int) $cd : 0;
+// If the year is not valid, set it to the current year
+// This excludes all the "too old", or "too far in the future"
+// calendar to displays (so search engines can handle this page too)
+if ($cy != 0 && !valid_year($cy)) {
+ $cy = date("Y");
+}
+
// We need to look up an event with an ID
if ($id) {
// Try to load event by ID and display header and info for that event
@@ -113,18 +120,27 @@ if (count($errors) > 0) {
$bom = mktime(0, 0, 1, $cm, 1, $cy);
$eom = mktime(0, 0, 1, $cm+1, 0, $cy);
-// Last month and next month
+// Link to previous month (but do not link to too early dates)
$lm = mktime(0, 0, 1, $cm, 0, $cy);
+if (valid_year(date("Y", $lm))) {
+ $prev_link = '%B, %Y', $lm);
+} else {
+ $prev_link = ' ';
+}
+
+// Link to next month (but do not link to too early dates)
$nm = mktime(0, 0, 1, $cm+1, 1, $cy);
+if (valid_year(date("Y", $nm))) {
+ $next_link = '%B, %Y', $nm);
+} else {
+ $next_link = ' ';
+}
// Print out navigation links for previous and next month
echo '
| %B, %Y | ', $lm), + "\n||
| ', $prev_link, ' | ', '', strftime('%B, %Y | ', $bom), - '%B, %Y | ', $nm), - "