Generating a ‘year-at-a-time’ calendar with calendar.class.php
In the previous post announcing the release of calendar.class.php I gave a quick example of how the class could be used to easily output multiple months. That example showed an interface made up of the previous, current and next month. Generating a full year at a time is just as easy to accomplish using a simple for loop. Here’s how…
To start off, include calendar.class.php. For this example I’ll also make use of the class’s built in linking behavior. The class has the ability to make days link to a url with the selected date appended. This is useful for allowing you to “walk through” the dates on the calendar. In order to set this up the script needs to know to look for a date passed through $_GET. If the date is present we’ll pass that to the constructor as our selected date, if not the script will fall back to a hard coded default date (2007-09-21 in this example). Then the Calendar object is created with the supplied date.
require_once('calendar.class.php'); $date = ( isset($_GET['date']) )? $_GET['date'] : '2007-09-21'; $example = new Calendar($date);
Another feature of the class that was demonstrated in the previous post, is the ability to pass in an array of dates to highlight on the calendar. Since this is a simple array of dates, you could easily load the array with dates obtained from a database query.
$example->highlighted_dates = array( '2007-08-06', '2007-08-15', '2007-09-04', '2007-09-07', '2007-09-12', '2007-09-28', '2007-10-10', '2007-10-20' );
Semantically speaking, we can describe a year as an ordered list of months. So I’ll start an ordered list to hold the months as list items, and give it an ID so it can be styled with CSS.
print("\n");
To build the year, we simply need to step through the 12 months of the year with a for loop. When the output_calendar method is called without arguments it will use the date initialized by the constructor. However we can override that behavior by passing specific year and month values. So, each time through the loop we’ll create a new list item and call the output_calendar method with the year and then increment the month to the current count of the loop.
That begs the question, where does the year value come from. It could simply be hard coded but the calendar produced would be locked into a set year. To keep the calendar more dynamic we’ll make use of one of the variables calendar.class.php initializes for us.
When the Calendar object is created it breaks the date provided to the constructor into its component parts ($year, $month, $day) and makes them available. So we can access the selected year as $example->year and pass that to output_calendar to produce the correct month.
for($i=1;$i<=12;$i++){ print("
The only thing left is to close the ordered list, and the “year-at-a-time” view is ready to go.
print(" ");
Will
Hi,
I just tried out your code and it is just what I have been looking for. Hopefully I can use it in a project I am about to start.
Thanks for sharing.
Will
Andy Woolfe
Excellent calender script it is just what i am looking for on my site.
I have tried to load onto my server all of your scripts but my calender doesn’t look like your http://style-vs-substance.com/projects/calendar/example_full_year.php
am i missing something out is it possible for you to compile all of the parts into 1 downloadable file to make it easier to download?
Jim Mayes
My intention really wasn’t to provide a ready made calendar, only to provide a versatile class people could use to generate calendars for their own uses. The class outputs ready to style html, you need to provide the CSS to give it the layout & look that you want. A sample and style sheet is included in the download for the general calendar display. The full year view requires a few additional style rules which can be seen by viewing source on that sample. You are more than welcome to reference my styles for help in creating your own. If I get time in the next day or so, I’ll try to put together a separate download for the year-at-a-time view. Also, if you have specific questions or issues, feel free to ask.
Adriaan
Hello i’am trying to get your calendar to work in Dutch….
With something like this….
/* zet de locale op Dutch */
setlocale (LC_ALL, ‘nl_NL’);
/* Output: vrijdag 22 december 1978 */
echo strftime (“%A %e %B %Y”, mktime (0, 0, 0, 12, 22, 1978));
but the data from the calendar stay’s in English.
Can help me on my way? Thanks
Here is the example: http://www.2theweb.nl/Concepts/kalender/cal.php
Adriaan
Jim Mayes
The class uses the date function to format all of the dates. In order to localize the dates it would need to be modified to use the strftime() instead. I believe most of the changes would mainly be needed in the output_calendar method for anything roughly based on the Gregorian calendar.
For example line 86 of the class:
That would need to be changed to:
As I’m not an expert on all the various calendars of the world. I’m sure there may be other issues cased by some of my “hard coded” formating. I am planning to rewrite as much of that code as possible for the next release. With that done, implementing localization might be a trivial task. I’m sure I’ll miss something and create a problem for some local out there, but I’ll certainly give it a try.
Clonix
Hi Adriaan,
had same probs with de_DE
for your Prob
/* zet de locale op Dutch */
setlocale (LC_ALL, ‘nld_nld’);
instead of
setlocale (LC_ALL, ‘nl_NL’);
may work
greets Clonix