Summary

Description

Simple PHP calendar

<< 2006, May >>
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 1 2 3 4

Version: v2.2
Author: Sylvain Baudoin
Licence: GPL (GNU General Public License)
Please, report all errors to [email protected]

Installation and use

Installation

  1. Copy the files calendar.php, calendar_js.php and calendar_locales.php in a single directory of your Web site.
  2. Copy the file calendar.css to your Web site but not necessarily in the same directory as the former files. For example, copy this file in the directory where you have placed all the stylesheets of your Web site.

You may rename the files:

Using the scripts

For a complete example, please refer to the demo page demo_en.php. The complete description of the calendar configuration parameters is provided in a dedicated paragraph.

PHP integration

Just proceed as follows in the PHP page where you want to display a calendar:

<html>
<head>
...
<link rel="stylesheet" href="calendar.css" type="text/css" />
...
</head>

<body>
...
<?php
...
// Call to the main calendar script
require_once("calendar.php");
// Settings
$params = array(...);
// Display
Calendar($params);
...
?>
<body>
<html>

The paths to the stylesheet and the script calendar.php must obviously be adapted in order to indicate the directory where you actually copied these files.

Setting the calendar is done using a assocative array. For example:

$params = array(
              "LANGUAGE_CODE" => "en",
              "FIRST_WEEK_DAY" => 1,
              "USE_SESSION" => true
          );
Calendar($params);

WARNING!!! if you enable the use of the session (USE_SESSION = true), you must create the session at the very beginning of your page because the calendar script will not do it.

If you want to put several calendars on the same page, do not bother include the script calendar.php again: you just have to define the new settings for each new calendar and call the function Calendar() with these parameters:

...
<?php
require_once("calendar.php");

// First calendar
$params1 = array(...);
Calendar($params1);
...
?>
...
<?php
// Second calendar
$params2 = array(...);
Calendar($params2);
?>
...

Since version 2.1, it is possible to get the calendar HTML code inside your PHP code by setting the parameter OUTPUT_MODE to "return". Example:

...
<?php
require_once("calendar.php");

$params1 = array(..., "OUTPUT_MODE" => "return", ...);
$html_calendar = Calendar($params1);
...
// Affichage du calendrier
echo $html_calendar;
...
?>
...

JavaScript integration

WARNING!!! for the JavaScript integration mode it is still required to have a PHP Web server in order to execute the scripts calendar.php and calendar_js.php. The purpose of this JavaScript integration is to be able to display the calendar on Web pages in which it is not possible to write PHP code.

Integration the calendar as a JavaScript is quite straightforward:

<html>
<head>
...
<link rel="stylesheet" href="calendar.css" type="text/css" />
...
</head>

<body>
...
<!-- We display the calendar -->
<script type="text/javascript" src="calendar_js.php?..."></script>
...
<body>
<html>

Setting the calendar is done by passing the calendar parameters along with their values as URL variables. For example, if you want to set the paremeters CSS_PREFIX to "foo_" and USE_SESSION to true, just do the following:

<script type="text/javascript"
  src="calendar_js.php?CSS_PREFIX=foo_&USE_SESSION=true">
</script>

Make sure that you have added the calendar's stylesheet to your page (see below, paragraph "Styling the calendar").

Settings and customisation

The calendar parameters

Setting the calendar is done by defining parameters. A default value is assigned to the parameters you have not defined:

Paramèter Description Default value
PREFIX prefix of the URL and session parameters of the calendar. Define a different value for each different calendar to display along in the same page. Do not start this prefix by a digit. "calendar_"
CSS_PREFIX prefix of the CSS classes used for styling the calendar. To be used to render the calendars for different styles. "calendar_"
DATE_URL if set, indicates a URL to use for making the days clickable. This URL is completed with the URL parameter indicated by the calendar parameter URL_PARAMETER. ""
URL_PARAMETER if the previous parameter (DATE_URL) is set, indicates the name of the URL parameter used to complete the URL DATE_URL and pass the clicked date. The date is formated according to the value of the parameter URL_DAY_DATE_FORMAT for the days and URL_MONTH_DATE_FORMAT for the month and year (title links). "date"
USE_SESSION set true to store the calendar rendering data in session. This allows this script to remember the date to be displayed while browsing among various pages.
WARNING!!! if you want to use sessions, you must create the session first at the very beginning of the page, because this script will not do it.
false
PRESERVE_URL when building the links for the "previous month" and "next month" links, tells if current URL must be preserved (true) and the date appended (?xx=yyy&...&date=...) or if the query string of the current URL must be discarded (false) and just add the date parameter (?date=...). true
JS tells if the calendar is integrated as a JavaScript (true) or not. false
JS_URL if the calendar is integrated as a JavaScript, this parameter gives the URL of the page that integrates the calendar. ""
FIRST_WEEK_DAY first day of the week: 1 for Monday, 2 for Tuesday, etc..., 7 or 0 for Sunday. 1 (Monday)
LANGUAGE_CODE 2-letter ISO code of the language to use for rendering the calendar. "fr" (French)
CLICKABLE_TITLE when DATE_URL is set, tells if the calendar title (i.e. the month + year at the top of the calendar) is also clickable. In this case, the date passed in the URL parameter has the format indicated by the parameter URL_MONTH_DATE_FORMAT. true
OUTPUT_MODE if set to "return", will make the function Calendar return the HTML code of the calendar. If set to "echo", the HTML code of the calendar is directly echoed into the response to the web browser. Use "return" if you want to get the HTML code of the calendar into a PHP variable and make some processing on it. "echo"
URL_DAY_DATE_FORMAT when DATE_URL is defined, tells the format of the calendar day dates passed in the URL. This format must comply with the format supported by the PHP function date. Has no effect if DATE_URL is not defined. "dmY" (ddmmyyyy)
URL_MONTH_DATE_FORMAT when DATE_URL is defined, tells the format of the month date passed in the URL for the calendar's title. This format must comply with the format supported by the PHP function date. Has no effect if DATE_URL is not defined. "mY" (mmyyyy)

Managing the links (clickable dates)

It is possible the have the days of the calendar clickable. To do so, you must define the parameter DATE_URL. In this case, all the dates of the calendar will be clickable. The URL of the dates are then constructed as follows:

<value of the parameter DATE_URL><?|&><value of the parameter URL_PARAMETER>=<day's date with the format URL_DAY_DATE_FORMAT>

For example, if you set DATE_URL to "/dir1/dir2/script.php?foo=bar", URL_PARAMETER to "event_date" and URL_DAY_DATE_FORMAT to "Ymd", then the day's URL will be the following:

/dir1/dir2/script.php?foo=bar&event_date=20060813
/dir1/dir2/script.php?foo=bar&event_date=20060814
...

The URL of the calendar title (i.e. the URL of the month) is constructed the same way but using URL_MONTH_DATE_FORMAT instead of URL_DAY_DATE_FORMAT.

With the current version of the calendar (as well as the previous versions) it is not possible the have only few dates clickable using the calendar parameters. If you wish to have only a given set of dates clickable, you must change the PHP code of the script calendar.php, for example to get the dates from a database.

Styling the calendar

The style of the calendar is entirely defined using CSS. You must declare the calendar's style sheet in the HTML code of your pages. For example, add the following line in the header of your Web pages if you have placed the default calendar's style sheet file in the root of your Web site:

<link rel="stylesheet" href="/calendar.css" type="text/css" />

Please, check also the demos provided with this script.

You may use the default stylesheet (the file calendar.css) or your own styles. For the complete description of the CSS classes used for styling the calendar, please refer to the file calendar.css. The names of the CSS classes are calculated using the prefix defined in the parameter CSS_PREFIX. This allows you to define two different styles for two calendars laid out in the same page. For instance, if you set the parameter CSS_PREFIX to foo_ for a calendar, then the CSS classes that will be used will be foo_title for the calendar title (year + month), foo_weekend for the weekend, etc.

If you want to hide the link that gets back to the current month located at the bottom of the calendar, set the CSS property display to none.

Internationalisation

Since version 2.0 of this program, the calendar may be rendered in different languages. The default language of the calendar is French. Version 2.0 comes with French, English, German, Spanish and Italian translations. Version 2.1 also comes with Chinese translations.

Reminder: if you want to change the language, use the calendar parameter LANGUAGE_CODE (see Settings) to give the ISO 639 code of the new language. You will find a list of the ISO 639 language codes on the Web site http://www.loc.gov/standards/iso639-2/langcodes.html. Example:

<?php
...
// Let's display the canlendar in German (ISO 639 code = de)
$params = array(..., "LANGUAGE_CODE" => "de", ...);
Calendar($params);
...
?>

To add a new translation to the calendar, proceed as follows:

  1. Find the ISO 639 code of the new language.
  2. Edit the file calendar_locales.php and complete the following three variables:
  3. Give the language code in the calendar parameter LANGUAGE_CODE.

Version history

Version Description
2.2
  • It is now possible to specify absolute URLs ("http://...") in the parameter DATE_URL. This was not supported by the previous versions.
  • Add the use of the parameter DATE_URL in the demos.
2.1
  • New parameters added: CLICKABLE_TITLE, OUTPUT_MODE, URL_DAY_DATE_FORMAT and URL_MONTH_DATE_FORMAT
  • "Back to current month" link added
  • Few bugs and XHTML non-conformities fixed
  • Documentation:
    • Paragraph "Styling the calendar" completed
    • New paragraph "Managing the links"
    • New documentation for the new parameters
  • New Chinese translations and demo
2.0
  • Change the way the calendar handles its parameters
  • Internationalisation support
  • New French and English documentation
1.1
  • Correction of a bug that made some days to be displayed twice
1.0
  • First version

Credits, licence and warranty

Copyright (c) 2005-2007 - Sylvain BAUDOIN ([email protected])

This PHP program may be redistributed and/or modified according to the terms of the GNU General Public License, as it has been published by the Free Software Foundation (version 2 and above).

This program is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.