Creating a calendar for the archive feature of the next redesign of this blog called for a combination of HTML, PHP, JavaScript and MySQL. Previously I’ve discussed the markup and CSS for the calendar; in this entry, I will show the PHP. A server-side language is used as the archive feature will be required for the new blog; Javascript will be added for extra, optional functionality.
Creating the calendar for an arbitrary month is the perfect role for a function. The renderCalendar
function requires just two variables: a year and month. The function will need to determine what day the 1st of the chosen month falls on, and the number of days in the month, filling in the days before and after, if applicable.
Here’s the complete code of the function, heavily adapted from work by Martin Breault.
<?php
function renderCalendar($strMonth, $strYear) {
$date = mktime(12, 0, 0, $strMonth, 1, $strYear);
$daysInMonth = date("t", $date);
$dayOffset = date("w", $date);
$nextday = 1;
$next = date("Y-m-",strtotime($strYear."-".$strMonth."-01 +1 months"));
if ($dayOffset > 0) {
$prevmonthdaystart = date("t",strtotime($strYear."-".$strMonth."-01 -1 months")) - ($dayOffset -1);
$prev = date("Y-m-",strtotime($strYear."-".$strMonth."-01 -1 months"));
}
$calendarWeeks = ceil(($daysInMonth + $dayOffset) / 7) - 1;
echo '<h1>'.date("F", $date).' '.$strYear.'</h1>';
for ($rowIndex = 0; $rowIndex <= $calendarWeeks; $rowIndex++) {
for ($colIndex = 1; $colIndex <= 7; $colIndex++) {
$currentDay = ($colIndex - $dayOffset) + ($rowIndex * 7);
if ($currentDay > $daysInMonth) {
$after = str_pad($nextday, 2 , "0",STR_PAD_LEFT)
echo '<time datetime="'.$next.$after.'" class="not month">’;
echo "<a href="#">'.$after.'</a></time>';
$nextday++;
} elseif ($currentDay < 1) {
echo '<time datetime="';
echo $prev.str_pad($prevmonthdaystart, 2 , "0",STR_PAD_LEFT);
echo "' class="notmonth"><a href="#">'$prevmonthdaystart.'</a>';
echo "</time>';
$prevmonthdaystart++;
} else {
$current = str_pad($currentDay, 2 , "0",STR_PAD_LEFT);
echo '<time datetime="'.$strYear.'-'.str_pad($strMonth, 2 , "0",STR_PAD_LEFT);
echo '-'.$current.'"><a href="#" id='.$currentDay.'" >';
echo $current.'</a></time>';
}
}
}
}
?>
mktime
converts a given date to standard Unix time. (As we don’t need information on a specific day, we use the 1st of the month as an arbitrary starting point). Given that, we can determine the number of days in the chosen month, and when the 1st of the month falls (w as a keyword in the date()
function provides an offset for the 1st day in the month: if the 1st falls on a Sunday, the result will be 0, Monday will be 1, Saturday 6, etc.)
$nextday
starts the count for the days after the current month; $next
generates the Unix time for the following month.
If the chosen month does not start on a Sunday, $prevmonthdaystart
determines the appropriate date from the previous month that does fall on a Sunday.
$calendarWeeks
is a bit of holdover from the original, table-based layout of the calendar, but still useful: it determines how many weeks there are in the chosen month. A series of for
loops generates the appropriate <time>
tags with date time
attributes, links, and visible dates inside each. (str_pad
ensures that leading zeros are added where appropriate).
That’s it! Link destinations are left blank for you to complete.
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.