Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Ajax calendar class

Here is AJAX Calendar, which can be used to display month calendars browsable using AJAX.

It can generate HTML and JavaScript to display a month calendar with links to browse the months using AJAX to avoid page reloading.

The class can make given days be displayed as links to event pages. The browsing may be restricted to given dates. The months and week day names are customizable. The presentation details may be customized using CSS.

Contents

Download

Example codes

<?php

/*********************/
//This is an example with all possible calendar customizations and 
//generating calendar and ajax request in the same file
/*********************/
//All methods are divided in 2 groups
//One should be used in same file or scope, where calendar will be generated
//Other should be used in same file or scope, where ajax request will be made
//The ones used in if(isset($_GET['ajax_calendar'])) clause 
//should be used within request file or scope
//The ones in else clause should be used within calendar generator file or scope

//you should always sepecify timezone when dealing with date function like used in calendar
date_default_timezone_set("Europe/Helsinki");

//declaring class instance
include("./calendar.class.php");
$calendar = new calendar();


if(isset($_GET['ajax_calendar']))
{
//if $_GET['ajax_calendar'] is set, it means request is beeing made 
//by ajax to get days  of other months or year
//make sure that you do not output anything to browser, 
//so you won't mess json structure of calendar
//other parameters that are passed with this request are
//$_GET['date'] - for date
//$_GET['month'] - for month
//$_GET['year'] - for year
//$_GET['start'] - which day to start week with
//and any other custom parameters added
//for this example it is parameter $_GET['param'] = "value"

    //set start month and year which is an oldest motnh to show
    $calendar->set_start_date(1,1987);
    //set end month and year which is a newest motnh to show
    $calendar->set_end_date(3,1987);
    
    //set url pattern, for all active days to be used as links 
    where $1 will be replaced by date, $2 replaces by month and $3 by year, 
    $calendar->set_url_pattern("javascript:alert('Date selected $3-$2-$1');");
    //$arr = array(1 => "#beginning_of_a_month", 30 => "#end_of_a_month");
    //$calendar->set_events($arr);
    
    //if ajax request is made to the same page, 
    //make sure nothing other is outputted to browser
    //you can specify request url in the file where you generate calendar
    //if nothing is specified then request will be made to the same page
    $calendar->process_request();
}
else
{
    //defining path to jquery and style css included in package
    echo '<script type="text/javascript" src="./jquery.js"></script>';
    echo '<style type="text/css" media="all">@import "./style.css";</style>';
    //array with custom month names
    $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    //applying custom month names
    $calendar->set_months($months);
    
    //array with custom days of week names
    $weekdays = array("M", "T", "W", "Th", "F", "S", "Sun");
    //applying custom days of week names
    $calendar->set_weekdays($weekdays);
    
    //define with which day to start week 0 - Monday, ..., 6 - Sunday
    //here in example it is Tuesday
    $calendar->set_week_start(1);
    
    //adding request parameter name and value which will appear in ajax request
    $calendar->add_request_param("param", "value");
    
    //getting errors
    $errors = $calendar->get_errors();
    if(!empty($errors))
    {
        foreach($errors as $error)
        {
            echo "<p>".$error."</p>";
        }
    }
    
    //generating calendar, you can specify date like in example
    //if no date is specified, than todays date will be used
    $calendar->generate_calendar(23,2,1987);
}
?>

Examples in action

Example scripts provided with package in action:

Method list

Some methods can be called only from calendar generator part, some can be called only from ajax part. You can check it by method type:

  • calendar part- for calendar generation part
  • ajax part - for ajax part

Get errors

Back to method list

Method nameget_errors()
DescriptionReturns array with errors or empty array if there was no errors
Example output
Array
(
    [0] => Incorrect amount of months
)

Set month names

Back to method list

Method nameset_months($arr)
Typecalendar part
DescriptionPurpose for changing language. Set month names starting from January to December
Input parametersarray $arr - array where each element is month name
Example inputset_months(array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

Set names for days of week

Back to method list

Method nameset_weekdays($arr)
Typecalendar part
DescriptionPurpose for changing language. Set names for days of week starting from Monday to Sunday
Input parametersarray $arr - array where each element is day name
Example inputset_weekdays(array("M", "T", "W", "Th", "F", "S", "Sun"))

Set from which day should week start

Back to method list

Method nameset_week_start($day)
Typecalendar part
DescriptionSets from which day should week start. Values from 0 to 6, where 0 represents Mondey and 6 - Sunay
Input parametersint $day - index of day to start week with
Example inputset_week_start(6)

Ajax request path

Back to method list

Method nameset_request_path($req)
Typecalendar part
DescriptionSets path where to send ajax request. Default falue - this same file where you're writing script
Input parametersstring $req - request path
Example inputset_request_path("./ajax.php")

Add parameters for ajax request

Back to method list

Method nameadd_request_param($name, $value)
Typecalendar part
DescriptionAdd parameters to pass along with ajax GET request. For example needed, if you want to generate multiple calendars with different settings from one ajax part file. Can't use class defined parameters:
  • 'date' - contains day of the month, example 12
  • 'month' - contains month, example 9
  • 'year' - contains year, example 2010
  • ,
  • 'start' - which day to start the week with, values from 0 to 6, where 0 represents Monday and 6 - Sunday
  • 'ajax_calendar' - to determine that request was made for ajax_calendar
Input parameters

string $name - parameter name

string $value - parameter value

Example inputadd_request_param("calendar_type", "main_page")

Set pattern for url generation in calendar days

Back to method list

Method nameset_url_pattern($pattern)
Typeajax part
DescriptionSet pattern to automatically generate urls for calendar days in your specified format. Juts pass url strings, whith following representation:
  • $1 - will be replaced with day date
  • $2 - with month
  • $3 - with year

For example, for the date 1st Januray 2011, url pattern: '/archive-$1-$2-$3' will be generated as '/archive-1-1-2011'

Input parameters

string $pattern - url pattern

string $value - parameter value

Example inputset_url_pattern("/archive-$1-$2-$3")

Set set links to specific dates

Back to method list

Method nameset_events($events)
Typeajax part
Description

Sets only specific dates as links. Alternative to set_url_pattern method, when you don't want to generate urls for all days in month. You must provide array, where keys are the day of the month in which to display links and values are URL for link. To determine which month is requested use class GET parameters described in add_request_param method.

NOTE: set_events() method works only if pattern isn't set using set_url_pattern method

Input parameters

array $events - current month days as keys and URL as values.

string $value - parameter value

Example inputset_url_pattern("/archive-$1-$2-$3")

Generate calendars

Back to method list

Method namegenerate_calendar($day = "", $month = "", $year = "")
Typecalendar part
Description

Generates calendar for provided month and year, marking day as today. If any of paramaters is empty, then current day, month or year will be used, for example method call generate_calendar(); will generate calendar for current day, month and year

Input parameters

int $day - day of the month.

int $month - what month to display on calendar

int $year - what year to display on calendar

Example inputgenerate_calendar(21, 12, 2112)

Set beginning of calendar

Back to method list

Method nameset_start_date($month = "", $year = "")
Typeajax part
Description

Set first month that should be displayed in calendar. It means calendar won't display any month that is before specified month and year.

Input parameters

int $month - start month to display on calendar

int $year - start year to display on calendar

Example inputset_start_date(1, 2000)

Set ending of calendar

Back to method list

Method nameset_end_date($month = "", $year = "")
Typeajax part
Description

Set last month that should be displayed in calendar. It means calendar won't display any month that is after specified month and year.

Input parameters

int $month - last month to display on calendar

int $year - last year to display on calendar

Example inputset_end_date(12, 2012)

Process ajax request

Back to method list

Method nameprocess_request()
Typeajax part
Description

Process ajax request to generate data for calendar in json format

Possible error messages

List of all errors and meanings

Error textMeaningSolution
Incorrect amount of monthsYou provided incorrect array for month namesProvide array with 12 elements
Incorrect amount of weekdaysYou provided incorrect array for weekdaysProvide array with 7 elements
Incorrect day of the weekYou provided incorrect index of the day of the weekProvide integer with values in interval from 0 to 6
Can not override main class parametersName for your parameter is already taken by class.Do not use "date", "month", "year", "start", "ajax_calendar" as parameter names

Latest changes

You will find predefined CSS file in package to customize your calendar design

Here is a default example:

/*table for calendar header with month, year etc*/
.ajax_calendar_header
{
    border:0;
    width:250px;
}
/*table cell that contains month and year*/
.ajax_calendar_main_date
{
    font-weight: bold;
    width: 60%;
    text-align: center;
}
/*table cell of link to switch to previous month when active 
(means can be switched to previuos months)*/
.ajax_calendar_prev_month_active 
{
    width: 10%;
    font-weight: bold;
}
/*customizing link inside of active cell (inactive cells doesn't have links)*/
.ajax_calendar_prev_month_active a
{
    text-decoration: none;
}
/*table cell of link to switch to previous month when inactive 
(means can not be switched to previous months because of start_date restriction)*/
.ajax_calendar_prev_month_inactive
{
    width: 10%;
    color: gray;
}
/*table cell of link to switch to previous year when active*/
.ajax_calendar_prev_year_active
{
    width: 10%;
    font-weight: bold;
}
/*customizing link inside of active cell (inactive cells doesn't have links)*/
.ajax_calendar_prev_year_active a
{
    text-decoration: none;
}
/*table cell of link to switch to previous year when inactive*/
.ajax_calendar_prev_year_inactive
{
    width: 10%;
    color: gray;
}
/*table cell of link to switch to next month when active*/
.ajax_calendar_next_month_active
{
    width: 10%;
    font-weight: bold;
}
/*customizing link inside of active cell (inactive cells doesn't have links)*/
.ajax_calendar_next_month_active a
{
    text-decoration: none;
}
/*table cell of link to switch to next month when inactive*/
.ajax_calendar_next_month_inactive
{
    width: 10%;
    color: gray;
}
/*table cell of link to switch to next year when active*/
.ajax_calendar_next_year_active
{
    width: 10%;
    font-weight: bold;
}
/*customizing link inside of active cell (inactive cells doesn't have links)*/
.ajax_calendar_next_year_active a
{
    text-decoration: none;
}
/*table cell of link to switch to next year when inactive*/
.ajax_calendar_next_year_inactive
{
    width: 10%;
    color: gray;
}
/*table with calendar itself*/
.ajax_calendar
{
    border:6px outset black;
    width:250px;
}
/*cells of calendar*/
.ajax_calendar td
{
    border: 1px solid black;
    text-align: center;
}
/*table row with days of the week*/
.ajax_calendar_weekdays
{
    font-weight: bold;
}
/*table cells with inactive days from previous or next months*/
.ajax_calendar_inactive_days
{
    color:black; 
    background-color: gray;
}
/*table cells with active days from currently selected month*/
.ajax_calendar_active_days
{
    color: blue;
}
/*table cell with currently selected date*/
.ajax_calendar_today_days
{
    color: red;
    background-color: yellow;
}

Latest changes

03.09.2010 - Added option to pass variables through ajax to php file

Rate us

Like our script? Rate it at Ajax > Hot Scripts

Scripts.com
RATE ME!

Try it out and Rate on PHPclasses.org

Support

PHP classes support forum or comments below


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus