PHP - Calendar, DatePicker Calendar - Original English version

- Credits go to TJ, the original Calendar Datepicker class developer on http://www.triconsole.com -

Check-out the main support page on triconsole.com

PHP - Calendar, DatePicker Calendar - Localized version

- Credits go to Ciprian Murariu, with the kind help of phpMyChat-Plus translators, based on 3.75 version of TJ's class above -
Appreciate the Localized Calendar Class and want us to improve?
Please consider making a donation to Localized Calendar Class developer (Ciprian). Thank you! :)

Download this multi-language version here - 149.35 Kb (Last updated on Thu, 13 Apr 2017 22:09). Read what's new...

Available in 44 languages and dialects: Arabian, Argentinian Spanish, Armenian, Brasilian Portuguese, Bulgarian, Catalan, Chinese (Simplified and Traditional), Czech, Danish, Deutch, Dutch, English (US & GB), Farsi (Persian), Finnish, French, Greek, Hebrew, Hindi (Devanagari), Hungarian, Indonesian (Bahasa Indonesia), Italian, Japanese, Korean, Lithuanian, Macedonian, Malaysian (Bahasa Melayu), Norwegian (BokmĂĄl & Nynorsk), Polish, Portuguese, Romanian, Russian, Serbian (Latin), Slovak, Spanish, Swedish, Thai, Turkish, Ukrainean, Uzbek, Vietnamese, Yoruba (and counting...)
Here is a plain Greek Sample! and here is the Sample source

Can't find your language on this list? Download this template, translate and send it to Ciprian by email!

Demo:

Select language :

Fixed Display Style

Date 1 :
  • Set default date to server date (Auto unset if date is disabled)
  • Set year navigate from 1890 to 2080
  • Allow date selectable from 1st Jan 1890 to 01 May 2045
  • Don't allow to navigate other dates from above
  • Disabled Sat and Sun
  • Disabled 01 Jan, 14 Apr and 25 Dec every year, recursively
  • Show calendar week
  • Set/show tooltips
  • Set timezone to Europe/Bucharest


  • (it works only if currrent date is not disabled)

Code:

<?php
$myCalendar = new tc_calendar("date1");
$myCalendar->setDate(date("d"), date("m"), date("Y"));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1890, 2080);
$myCalendar->dateAllow("1890-01-01", "2080-05-01", false);
$myCalendar->disabledDay("sat");
$myCalendar->disabledDay("sun");
$myCalendar->setSpecificDate(array("2039-04-01", "2011-04-14", "2011-04-25"), 0, "month");
$myCalendar->showWeeks(true);
$myCalendar->setTimezone("Europe/Bucharest");
$myCalendar->writeScript();
?>

DatePicker Style

Date 2 :
 Select
  • Set default date to server date (Auto unset if date is disabled)
  • Don't show the calendar icon, just the Select link
  • Don't autohide the calendar window (displays the Close button)
  • Set year navigate from 1960 to current year
  • Allow date selectable from 01 March 1960 to current date
  • Allow to navigate other dates from above
  • Disabled Sun
  • Disabled 01 Jan, 14 Apr and 25 Dec every year, recursively
  • Custom alert when calendar's value has been changed
  • Set TimeZone to Europe/Bucharest (displayed in About info box)

Code:

First, we set a function for use with class setOnChange() example:
<script language="javascript">
<!--
function myChanged(v){
alert("Hello, value has been changed : "+document.getElementById("date2").value+" ["+v+"]");
}
//-->
</script>
then:
<?php
$myCalendar = new tc_calendar("date2", true);
$myCalendar->setAutoHide(false);
$myCalendar->setDate(date("d"), date("m"), date("Y"));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1960, date("Y"));
$myCalendar->dateAllow("1960-01-01", date("Y-m-d"));
$myCalendar->setAlignment("left", "bottom");
$myCalendar->disabledDay("sun");
$myCalendar->setSpecificDate(array("2011-04-01", "2011-04-14", "2010-12-25"), 0, "year");
$myCalendar->setOnChange("myChanged('Test message')");
$myCalendar->setTimezone("Europe/Bucharest");
$myCalendar->writeScript();
?>

DatePicker with no input box

Date 3 :
  • No default date
  • Show the calendar icon
  • Set year navigate from 1945 to current year
  • Allow date selectable from 13 May 2008 to today
  • Allow to navigate other dates from above
  • Date input box set to false

Code:

<?php
$myCalendar = new tc_calendar("date3", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1945, date("Y"));
$myCalendar->dateAllow("2008-05-13", date("Y-m-d"));
$myCalendar->setAlignment("left", "bottom");
$myCalendar->writeScript();
?>

Date Pair Example

Date 4 interval from:

 

Code:

<?php
	// Example 1 (1 week interval before current date)
	function getDaysInWeek ($weekNumber, $year, $dayStart = 1) {
		// Count from '0104' because January 4th is always in week 1
		// (according to ISO 8601).
		$time = strtotime($year . '0104 +' . ($weekNumber - 1).' weeks');
		// Get the time of the first day of the week
		$dayTime = strtotime('-' . (date('w', $time) - $dayStart) . ' days', $time);
		// Get the times of days 0 -> 6
		$dayTimes = array ();
		for ($i = 0; $i < 7; ++$i) {
			$dayTimes[] = strtotime('+' . $i . ' days', $dayTime);
		}
		// Return timestamps for mon-sun.
		return $dayTimes;
	}

	$thisweek = date('W');
	$thisyear = date('Y');
	$dayTimes = getDaysInWeek($thisweek, $thisyear);

	$date4_default = date('Y-m-d', $dayTimes[0]);
	$date5_default = date('Y-m-d', $dayTimes[(sizeof($dayTimes)-1)]);
	//----------------------------------------

	// Example 2 (30 days interval before current date)
	$GapDays = 60 * 60 *24 * 30;
	$date4_default = date("Y-m-d");
	$date5_default = date("Y-m-d",time() - $GapDays);
	//----------------------------------------
	
$myCalendar = new tc_calendar("date4", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date("d", strtotime($date4_default)), date("m", strtotime($date4_default)), date("Y", strtotime($date4_default)));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->setAlignment("left", "bottom");
$myCalendar->setDatePair("date4", "date5", $date5_default);
$myCalendar->writeScript();

$myCalendar = new tc_calendar("date5", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date("d", strtotime($date5_default)), date("m", strtotime($date5_default)), date("Y", strtotime($date5_default)));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->setAlignment("right", "bottom");
$myCalendar->setDatePair("date4", "date5", $date4_default);
$myCalendar->writeScript();
?>

Color Themes (v3.75)

Sample Code:

<?php
$myCalendar = new tc_calendar("date_theme", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(1, date('m'), date('Y'));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(2010, 2020);
$myCalendar->setDateFormat('j F Y');
$myCalendar->setAlignment('left', 'bottom');
$myCalendar->setAutoHide(true, 10000); //10 secs
$myCalendar->showWeeks(true);
$myCalendar->startMonday(true);
$myCalendar->setTheme('theme1');
$myCalendar->writeScript();
?>

Example of submiting value: click here


Copyright

This calendar datepicker is totally free to use. You can use it in all of your projects without any costs.

How to setup:

Only 2 steps required for setup and use this calendar component.

Define and check for the selected localization of the calendar. Put the following code in the header of the page.

<?php
// Request selected language
$hl = (isset($_POST["hl"])) ? $_POST["hl"] : false;
if(!defined("L_LANG") || L_LANG == "L_LANG")
{
	if($hl) define("L_LANG", $hl);

	// You need to tell the class which language you want to use.
	// L_LANG should be defined as en_US format!!! Next line is an example, just put your own language from the provided list
	else define("L_LANG", "he_IL"); // Ebraic example - change the red value to your desired language (from the list provided)
}
?>

Create form element in the html and put the following code:

<form action="somewhere.php" method="post">
<?php
//get class into the page
require_once("classes/tc_calendar.php"); //instantiate class and set properties $myCalendar = new tc_calendar("date1", true);
$myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate(1, 1, 2000);
//output the calendar
$myCalendar->writeScript();
?> </form>

How to get the value?

To get the date selected in calendar by php after submit the form, simple write script as the following:

<?php
$theDate = isset($_POST["date1"]) ? $_POST["date1"] : "";

?> 

The above script should be on another file that html form point to. The parameter "date1" is the object name that you set in the code at the time calendar construct. See further in Functions and Constructor below.

To get the date selected by javascript on the current page, write script as the following:

<form action="somewhere.php" method="post" name="form1">
<?php
//get class into the page
require_once("classes/tc_calendar.php"); //instantiate class and set properties $myCalendar = new tc_calendar("date1", true);
$myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate(1, 1, 2000);
//output the calendar
$myCalendar->writeScript();
?> </form> //use javascript to get the value <script language="javascript"> <!-- function showDateSelected(){ alert("Date selected is "+document.form1.date1.value); } //--> </script> //create link to click and check calendar value <a href="javascript:showDateSelected();">Check calendar value</a>

Calendar Properties

rtl - specify the right-to-left direction of calendar (already implemented in the localized version per each language)

Ex. $myCalendar->rtl = true;

zindex - specify the zindex value to calendar

Ex. $myCalendar->zindex = 99;

Calendar Functions

Constructor

tc_calendar (string bindObjectName)

tc_calendar (string bindObjectName, boolean date_picker)

tc_calendar (string bindObjectName, boolean date_picker, bool show_input)

date_picker default value is false.
show_input default value is true

Methods

autoSubmit (bool flag, string form_name, string target_url)

Specify the calendar to auto-submit the value. Default value of autosubmit is false

To set calendar auto submit, specify flag to true and you need to specify either form_name or target_url to make the calendar to perform autosubmit correctly

Ex 1. $myCalendar->autoSubmit(true, "myForm");
//assume that the calendar is in the form named "myForm", then tell the calendar to auto submit the value (other values in myForm will be submitted together by html post method)

Ex 2. $myCalendar->autoSubmit(true, "", "anywhere.php");
//tell the calendar to submit the value to "anywhere.php". This method will submit only calendar value via html get method

dateAllow (date from, date to, bool navigate_not_allowed_date)

Specify date range allow to select. Other dates from input will be disabled. The parameter navigate_not_allowed_date will handle the user to navigate over the disable date, default is true (means allow)

Specify both date from and to will set range of date user can select.
Specify only date from or to will set range from/upto year set by setYearInterval method.

Ex 1. $myCalendar->dateAllow("2008-05-13", "2010-03-01", false); //allow user select date from 13 May 2008 to 1 Mar 2010
Ex 2. $myCalendar->dateAllow("2008-05-13", "", false); //allow user select date from 13 May 2008 upto whatever can navigate
Ex 3. $myCalendar->dateAllow("", "2010-03-01", false); //allow user select date from whatever can navigate upto 1 Mar 2010

disabledDay (string day)

Specify day column to be disabled. Possible day value: Mon, Tue, Wed, Thu, Fri, Sat, Sun

Ex. $myCalendar->disabledDay("Sun"); //disable Sunday column

getDate ()

Get the calendar value in date format YYYY-MM-DD

Ex. $myCalendar->getDate(); //return 2009-06-19

setAlignment (string horizontal_align, string vertical_align)

Optional: Set the alignment of calendar datepicker with the calendar container. The value of horizontal alignment can be "left" or "right" (default) and the value of vertical alignment can be "top" or "bottom" (default).

Ex. $myCalendar->setAlignment("left", "top");

setAutoHide (bool auto, int delay_time)

Optional: Set an auto hide of calendar when not active. The calendar has default value of auto hidden since version 3.65 with default delay time of 1000 (1 second)

Ex 1. $myCalendar->setAutoHide(true, 3000); //auto hide in 3 seconds
Ex 2. $myCalendar->setAutoHide(false);

setDate (int day, int month, int year)

Optional: Set default selected date to the value input. For month parameter: January=1 and December=12

Ex. $myCalendar->setDate(15, 6, 2005); //Set the date to 15 June 2005

setDateFormat (string format)

Optional: Set the format of date display when no input box. It only accepts the php date() format characters: http://php.net/manual/en/function.date.php. Apply with "showInput" function

Ex. $myCalendar->setDateFormat("j F Y"); //date will display like "9 September 2009"

setDatePair (string calendar_name1, string calendar_name2, string default_pair_value)

Optional: Set a date pair link to calendar object. This function will work when 2 calendars are setup together.

Ex 1. $myCalendar->setDatePair("date1", "date2"); //This means when the value of calendar "date1" is set, the calendar "date2" will not allow to select the date prior to "date1"
Ex 2. $myCalendar->setDatePair("date1", "date2", "2011-03-26"); //suppose you are setting this value on date1 object, "2011-03-26" will be assigned as default date of date2 and calculated

setIcon (string url)

Optional: Set icon in date picker mode. If the icon is not set the date picker will show text as link.

Ex. $myCalendar->setIcon("images/iconCalendar.gif");

setHeight (int height) - deprecated since v2.9 - auto sizing applied

Optional: Set height of calendar. Default value is 205 pixels

Ex. $myCalendar->setHeight(205);

setOnChange (string value)

Optional: Set the onchange javascript function to calendar

Ex. $myCalendar->setOnChange("javascriptFunctionName()");

setPath (string path)

Optional: Set the path to the "calendar_form.php" if it is not in the same directory as your script. The path string is a relative path to the script file.

Ex. $myCalendar->setPath("folder1/");

setSpecificDate (array dates, int type, string recursive)

Optional: Set the specific to enable or disable.

dates: the array of date, and date should be in format YYYY-MM-DD
type: enable or disable the date specified. 0=disable, 1=enable
recursive: "" (blank)=no recursive, calendar will process only on exact date, "month"=monthly recursive, or "year"=yearly recursive

Ex 1. $myCalendar->setSpecificDate(array("2011-04-22"));
Ex 2. $myCalendar->setSpecificDate(array("2011-04-22"), 0, "month"); //disable date 22 every month
Ex 3. $myCalendar->setSpecificDate(array("2011-04-22"), 0, "year"); //disable 22 Apr on every year

setToolTips (array dates, string tooltip, string recursive)

Optional: Set the tooltip info to be displayed on certain dates, recursively or not.

dates: the array of dates, and date should be in format YYYY-MM-DD
tooltip: string/text to be displayed when mouse hover the info.gif icon in the top-right corner of each day
recursive: "" (blank)=no recursive, calendar will process only on exact date, "month"=monthly recursive, or "year"=yearly recursive
hint: the function can be called any number of times, like all 5 examples below, together; multi-tooltips will be displayed on multi-lines, in this main order: non-recurrent, monthly then yearly, and then in the order of calling the function.

Ex 1. $myCalendar->setToolTips(array("2013-08-06", "2013-08-15", "2013-08-25"), "Tooltip Test");
Ex 2. $myCalendar->setToolTips(array("2013-06-06", "2013-06-01", "2013-06-05"), "Monthly Tooltip", "month");
Ex 3. $myCalendar->setToolTips(array("2013-06-06", "2013-08-11", "2013-09-15"), "Second Monthly Event", "month");
Ex 4. $myCalendar->setToolTips(array("2013-06-06", "2013-01-01", "2013-12-25"), "Yearly Event", "year");
Ex 5. $myCalendar->setToolTips(array("2013-08-06", "2013-08-15", "2013-08-25"), "Second Tooltip test");

Known limits:

  • only first 40 rows can be displayed in a tooltip, due to the html limitation. So make sure you keep the number of events to be displayed for a single date less than 41;

setText (string text)

Optional: Set text to display. The text will show in date picker mode when the icon is not set.

Ex. $myCalendar->setText("Click Me");

setTheme (string theme_name) - v3.75 - New!

Optional: Set calendar color theme. Theme name is represented for css folder name such as 'theme1' and theme folder must exist as {calendar_folder}/css/(theme_name)/calendar.css

Ex. $myCalendar->setTheme("theme1");

setWidth (int width) - deprecated since v2.9 - auto sizing applied

Optional: Set width of calendar. Default value is 150 pixels

Ex. $myCalendar->setWidth(150);

setYearInterval (int year_start, int year_end)

Optional: Set the year start and year end display on calendar combo box. Default value is +15 and -15 from current year (30 years)

Ex. $myCalendar->setYearInterval(1970, 2020);

showInput (bool flag)

Optional: Set the input box display on/off. If showInput set to false, the date will display in panel as example above "DatePicker with no input box". Default value is true

Ex. $myCalendar->showInput(false);

showWeeks (bool flag)

Optional: Display week numbers on calendar upon setting. Default value is false.

Ex. $myCalendar->showWeeks(true);

startDate (bool date_number)

Optional: You can choose any day as a start day of week. The possible values are 0 (for Sunday) through 6 (for Saturday). Default value is 0 (Sunday).

Ex. $myCalendar->startDate(6); //start day of week is Sat.

startMonday (bool flag) - deprecated since v3.61 - use startDate instead

Optional: Set whether the calendar will be start on Sunday or Monday. Set flag to true means the calendar will display first date as Monday, otherwise false will display first date as Sunday. Default value is false.

Ex. $myCalendar->startMonday(true);

writeScript()

Write the output calendar to the screen

© Triconsole (triconsole.com)

Visitor Statistics
» 1 Online
» 6 Today
» 11 Yesterday
» 6 Week
» 391 Month
» 2544 Year
» 19441 Total
Record: 74 (04.03.2015)
Free PHP Counter