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'));

View This Example

The formatting and styling of the calendar table is done purely through CSS. Style yours to fit your own design needs.

6 Comments

  1. luiz

    # October 18, 2007 - 10:56 am

    I am trying to use the calendar, but i do not understand how to link html pages to a specific days.

  2. Jim Mayes

    # October 19, 2007 - 4:43 pm

    To construct the day links the class uses the value set in $link_to and then appends that day’s date. This happens on line 167 of the class which simply does this:

    $output .= "link_to . "?date=" . $day_date . "\">";
    

    By default $link_to will have the value of PHP_SELF unless you override that value, meaning the days will be links back to the calling page, just with a specific date appended to the link.

    To override this with your own link you simply need to pass a new value into $link_to AFTER initializing the calendar and BEFORE calling the output_calendar() method. For example, to make the days link to a page called day.php, your code might look something like this:

    $example = new Calendar();
    $example->link_to = 'day.php';
    print($example->output_calendar());
    

    At that point your day.php could retrieve the selected date from $_GET['date'] and you could use that to query your database for whatever corresponding records match the provided date.

    The way the class is now, all of the days will link to the same page. It is not currently possible to do something like link 10/17/2007 to page1.php and 10/18/2007 to page2.php.

    I’m considering adding the ability to let dates provided in the selected date array specify individualized links in a future version of the class. That functionality kind of seems a little too specialized, so I’m not sure it belongs in this class. We’ll see.

    Hope that helps, if not post more details.

  3. rastamate

    # October 1, 2008 - 1:32 pm

    Hi Jim.

    First I wanna thank you for this great class!
    I’ve used it to program an ovulation calendar.

    There I had to show National Holidays and that’s how I’ve implemented them. Maybe something like this could be implemented in a future version:

    In the class statement right after var $today_date_class = ‘today’ I’ve added:

    var $mark_feriados = TRUE;
    var $feriados_class = ‘feriados’;

    Right after the block: //–compare day and add classes for matches I’ve added:

    //——————– Implementacion Feriados
    // Feriados
    $feriado1 = “01-01″;
    $feriado2 = “01-06″;
    $feriado3 = “04-19″;
    $feriado4 = “05-01″;
    $feriado5 = “06-24″;
    $feriado6 = “07-05″;
    $feriado7 = “07-24″;
    $feriado8 = “10-12″;
    $feriado9 = “12-25″;
    $feriado10 = “12-31″;

    if( $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado1
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado2
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado3
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado4
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado5
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado6
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado7
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado8
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado9
    || $this->mark_feriados == TRUE && $day_date == $year . “-” . $feriado10)
    {
    $classes[] = $this->feriados_class;
    //———————– End Feriados

    I’m sure this could be done much better, but it works.

    Now I’m able to style the National Holidays over the CSS tag .feriados without interfering with the highlight or selected classes.

    Saludos de Venezuela
    Peter

  4. andi

    # October 7, 2008 - 2:22 am

    very useful code, thanks. I hope i am able to implement it.

  5. Torben

    # March 29, 2009 - 7:32 am

    Hi.

    First of all thank you for a super class. I’m using the class to display available dates in my parents countryside cottage. I would like the class also to output the week number as a column on the left. Any quick pointers to how I can modify the class to do this? The class coding is a bit overwhelming for a php newbie like me.

    Kind regards
    Torben. – From Denmark

  6. Martha

    # December 22, 2011 - 1:43 pm

    Hi, thank you for this calendar, just what I need, except I also need links to go back and forward from month to month. Any suggestions on how to do this?

    thanks!!!

Leave a Reply