PHP Calendar Class
Updates
Jan 18, 2006: Editted documentation, expanded the example at the end for extending and customizing the output.
Feb 2, 2003: Fixed a bug that cause the calendar to display improperly if the month begins on a sunday. Added 4 new public methods: getStartTime(), getEndTime(), setMonth(), and setYear().
Feedback and comments are welcome
Background
I recently needed to whip up a quick and simple calendaring web application to keep track of some upcoming events. I did a search for php calendaring classes but wasn't quite satisfied with what I found.
Cascades Simple Calendar class looked good but would require some hacking to get event information listed in the body of the calendar. Keith Devens has a good calendar script too but it is procedural and just allows you to link days to other pages. Most others were full-fledged calendaring solutions with their own backends and I already had an events database setup.
Figuring that it couldn't be that hard to roll my own, after an hour with the online PHP manual I had a working php calendar class. This class does one thing, it creates a single monthly calendar for a given month and year. If you need a 12 month calendar, it'd be trivial to loop and create twelve calendars.
A simple example
After downloading the class and including it in your PHP script, you're ready to start using it. The constructor requires two paramets, a year and month. The following code produces a very plain looking calendar.
require_once( 'class.Calendar.php' );
2
$cal = new Calendar ('2004', '3' );
3
echo "<b>".$cal->getFullMonthName()."</b>";
4
echo $cal->display();
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Customizing the display
You can use two methods to change how the calendar is displayed. Use setTableWidth to define how wide to make the table, in pixels or as a percentage of the screen. Use setDayNameFormat to change how weekdays are displayed in the header row using strftime formats.
require_once('class.Calendar.php');
2
$calendar = new Calendar ('2004', '4');
3
$calendar->setTableWidth('50%');
4
$calendar->setDayNameFormat('%A');
5
echo "<b>".$calendar->getBriefMonthName().</b>";
6
echo $calendar->display();
| Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Final Example
You can use CSS selectors to control the formatting of all aspects of the calendar, including adding borders and how empty boxes display. Lets wrap our talbe in a div named calendar and use the following styles.
#calendar table {
border:1px solid #fff;
padding: 0;
margin:20px;
}
/* the day headers across the top */
#calendar th {
background: #565;
color: #fff;
margin: 3px;
padding: 2px 5px;
}
/* how days this month will look, make dates appear in top right */
#calendar td {
background: #eee;
color: #000;
padding: 2px;
margin:0;
height:60px;
text-align:right;
vertical-align:top;
border:1px solid #fff;
}
/* these are the days falling outside this month */
#calendar td.notInMonth {
background: #999;
}
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Extending the Class
By extending the class, you can customize its behavior and its output. When the calendar output is being generated, table cells for the month are generated by the dspDayCell() method. The method is sent one parameter - the day of the month. Take a look at class.Calendar_events.phps to see how to build a simple calendar that displays links to event details in day cells.
To extend the class to handle events, we need to add two methods and customize how days are displayed.
First, we a new class that will inherit most of its methods from the main Calendar class, using the 'extends' keyword:
require_once('class.Calendar.php');
2
class Calendar_Events extends Calendar
We also need one public method to allow us to add events and store them internally in an array. Another method, will return the events for a given day. There method signatures are:
function addEvent($day, $title, $link = '')
2
function getDaysEvents($day)
Finaly, we create our own dspDayCell method that will print out the links to events for a given day along with the numeral for the day (code ommited see class.Calendar_events.phps).
function dspDayCell($day)
2
{
3
if ($events = $this->getDaysEvents($day))
4
{
5
// print out $day
6
// loop through events and output the links
7
}
8
else
9
{
10
// just print out $day
11
}
12
}
Putting it all together
Now, to use the Event calendar you have to get data into it. Normally, this would come from a text file, database, or similar data source. For simplicity, let's assume you have an array of events information.
$events[] = array('day' => 4, 'name' => 'Big Important Event', 'link' => http://www.oscarm.org/")
2
$events[] = array('day' => 5, 'name' => 'Followup Important Event', 'link' => http://www.google.com/")
3
$events[] = array('day' => 15, 'name' => 'The Ides', 'link' => http://www.forumone.com/")
4
$events[] = array('day' => 31, 'name' => 'End of Month', 'link' => http://www.soccerblogs.net/")
5
6
// create a calendar, add events
7
$Calendar = new Calendar_Events('2004', '4');
8
9
// add events to calendar
10
foreach ($events as $event)
11
{
12
$Calendar->addEvent($event['day'], $event['name'], $event['link']);
13
}
14
15
$calendar->setTableWidth('50%');
16
$calendar->setDayNameFormat('%A');
17
18
// display
19
echo "<b>".$calendar->getBriefMonthName().</b>";
20
echo $calendar->display();













