PHP - Calendar, DatePicker Calendar

Demo:

Fixed Display Style

Date 1 :
  • Set default date to today date (server time)
  • Set year navigate from 1970 to 2020
  • Allow date selectable from 13 May 2008 to 01 March 2015
  • Not allow to navigate other dates from above
  • Disabled Sat and Sun

Code:

<?php
$myCalendar = new tc_calendar("date2");
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date('d'), date('m'), date('Y'));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->dateAllow('2008-05-13', '2015-03-01', false);
$myCalendar->startMonday(true);
$myCalendar->disabledDay("Sat");
$myCalendar->disabledDay("sun");
$myCalendar->writeScript();
?>

DatePicker Style

Date 2 :
  • Default date set to 01 March 1960
  • Set year navigate from 1960 to 2015
  • Allow date selectable from 01 January 1960 to 01 March 2015
  • Allow to navigate other dates from above

Code:

<?php
$myCalendar = new tc_calendar("date1", true);
$myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate(01, 03, 1960);
$myCalendar->setPath("./");
$myCalendar->setYearInterval(1960, 2015);
$myCalendar->dateAllow('1960-01-01', '2015-03-01');
$myCalendar->writeScript();
?>

DatePicker with no input box

Date 3 : 26 April 2024
  • Default date to current server date
  • Set year navigate from 2000 to 2015
  • Allow date selectable from 13 May 2008 to 01 March 2015
  • Allow to navigate other dates from above
  • Date input box set to false

Code:

<?php
$myCalendar = new tc_calendar("date5", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date('d'), date('m'), date('Y'));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(2000, 2015);
$myCalendar->dateAllow('2008-05-13', '2015-03-01');
$myCalendar->setDateFormat('j F Y');
$myCalendar->writeScript();
?>

Date Pair Example

from:
22-Apr-2024
to
28-Apr-2024

Code:

<?php					
$myCalendar = new tc_calendar("date3", true, false);
$myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate(date('d', strtotime($date1)) , date('m', strtotime($date1)) , date('Y', strtotime($date1)));
$myCalendar->setPath("./");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->writeScript();

$myCalendar = new tc_calendar("date4", true, false);
$myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate(date('d', strtotime($date2)) , date('m', strtotime($date2)) , date('Y', strtotime($date2)));
$myCalendar->setPath("./");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->writeScript();
?>

How to setup:

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

Put the javascript file(.js) in the head section or somewhere else but it should be include once in a page.

<head>
<script language="javascript" src="calendar.js"></script>
</head>

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($_REQUEST["date1"]) ? $_REQUEST["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>

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

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. Apply with 'showInput' function

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

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

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/");

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

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

startMonday (bool flag)

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)