calendar.class.php v1 released
As work progresses on the firepaper project, I found myself in need of a solid PHP class to generate calendar interfaces for things like picking dates for reviewing logged time, scheduling tasks, etc. I looked around for a while at how others were dealing with creating calendar displays and wasn’t satisfied with what I found.
A lot of the examples I found used mathematic calculations to get the days to line up on the proper day of the week and fall into the correct weeks (rows of the table). This seems unnecessary considering PHP’s date function provides everything needed to do the job. In addition I wanted the output to be semantically meaningful, easily styled and of course valid XHTML 1.0 Strict. So, I created my own calendar creation class, calendar.class.php, and I’m releasing it into the wild under a Creative Commons Attribution-Share Alike 3.0 License.
Default behavior of the class (when called with no arguments) is to display the current month with today’s date indicated with a CSS class for easy styling. For more advanced uses the constructor and output method accept arguments that control the month to be displayed in various ways. There are also several exposed variables that allow for controlling linking behavior for the days, allowing you to provide your own class names for the different elements and provide an array of dates to highlight on the calendar.
For full documentation and an example of the most simple use, head over to the calendar.class.php download page.
In the meantime, here is an example to show how the class can be used in a more advanced situation. The example will display a month calendar for a specific date (September 18, 2007 in this instance) with the preceding and following months also displayed. I’ll also pass in an array of dates (which could just as easily have been created from a database query) to be highlight.
require_once('calendar.class.php'); $example = new Calendar('2007-09-18'); $example->highlighted_dates = array( '2007-08-06', '2007-08-15', '2007-09-04', '2007-09-07', '2007-09-12', '2007-09-18', '2007-09-28', '2007-10-10', '2007-10-20' ); print($example->output_calendar('2007', '08')); print($example->output_calendar()); print($example->output_calendar('2007', '10'));
The formatting and styling of the calendar table is done purely through CSS. Style yours to fit your own design needs.