PHP - Calendar, Datepicker Calendar

The calendar component written in php script. It contains 2 modes: normal display calendar and date picker. See the demo below to check whether your browser support this script or not.
 
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 2010
  • Not allow to navigate other dates from above

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 2010
  • Allow to navigate other dates from above

DatePicker with no input box

Date 3 : 5 October 2009
  • Default date to 5 October 2009
  • Set year navigate from 2000 to 2015
  • Allow date selectable from 13 May 2008 to 01 March 2010
  • Allow to navigate other dates from above
  • Date input box set to false

Date Pair Example

from:
08-Mar-2010
to
14-Mar-2010

Example of submiting value: click here


Download zip file: calendar.zip (17.19 KB.)
Version 2.8 (Last update 5 December 2009)


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) << Latest Issue

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

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) << Latest Issue

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)

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)

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) << Latest Issue

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

 
 
Visitor Review  Write a review

Jen Rasmussen on 09 Mar 2010 12:42 reply

Hi, Please help.
I am trying to use calendar twice in form and update dates to mysql dbase.

code on form page (these forms reside within a larger form)

<td class='listtext' width='140' align='right'>Ship Date</td>
<form name='form1' method='post' action='../calendar/calendar_form.php'>
<td class='bodytext'>";

$myCalendar = new tc_calendar('date1', true);
$myCalendar->setIcon('../calendar/images/iconCalendar.gif');
$myCalendar->setDate(01, 3, 2010);
$myCalendar->setPath('../calendar');
$myCalendar->setYearInterval(2010, 2020);
$myCalendar->dateAllow('2010-03-01', '2020-12-31');
//$myCalendar->setHeight(350);
//$myCalendar->autoSubmit(true, 'form1');
$myCalendar->writeScript();

echo "</td>
</form>

</tr>
<tr>

<td class='listtext' align='right'>Freight Cost</td>
<td><input name='shipCost' value='$shipCost' class='bodytext' size='13' /></td>

<td class='listtext' width='140' align='right'>Est. Delivery Date</td>
<form name='form1' method='post' action='../calendar/calendar_form.php'>
<td class='bodytext'>";

$myCalendar = new tc_calendar('date3', true);
$myCalendar->setIcon('../calendar/images/iconCalendar.gif');
$myCalendar->setDate(01, 3, 2010);
$myCalendar->setPath('../calendar');
$myCalendar->setYearInterval(2010, 2020);
$myCalendar->dateAllow('2010-03-01', '2020-12-31');
//$myCalendar->setHeight(350);
//$myCalendar->autoSubmit(true, 'form2');
$myCalendar->writeScript();

echo "</td>
</form>

Code on exec page:

$shipDate = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
$deliveryDate = isset($_REQUEST["date3"]) ? $_REQUEST["date3"] : "";

//echo $shipDate;
echo $deliveryDate;


I am able to successfully receive a variable and update for date1 but no no data transmits from form for date3.

Jen Rasmussen on 09 Mar 2010 12:48 reply

I'm also trying to populate the form with initial values from the database (in the case it's been updated previously)... if anyone has advise on that it would be great also :)


Jen Rasmussen on 09 Mar 2010 16:50 reply

I figured this part out, still having problems with more than one calendar, however, not sending the 2nd date to the next page... help :)

Jen Rasmussen on 09 Mar 2010 13:38 reply

also i realize i have 'form1' in both forms but i've tried with 'form2' in 2nd form as well and it doesn't make a diff

TJ on 10 Mar 2010 00:00 reply

hi, you should make both date together in one form like

<form ...>
[date1 script]
and then
[date2 script]
</form>

so when you submit the form, you will get both values

Jen Rasmussen on 10 Mar 2010 09:18 reply

TJ,

that worked perfect. 2 seconds to fix and i racked my brain all day yesterday.

You rock, thanks so much!! ;p
Jen Rasmussen on 10 Mar 2010 09:22 reply

here's my final code for any future seekers. the form includes two calendar pickers, a ship date and delivery date, stores the information in mysql, and populates a previously stored date from the dbase in the form.

Form page:
<td class='listtext' width='140' align='right'>Ship Date</td>
<form name='form1' method='post' action='../calendar/calendar_form.php'>
<td class='bodytext'>";


$myCalendar = new tc_calendar('shipDate', true, false);
$myCalendar->setIcon('../calendar/images/iconCalendar.gif');
$myCalendar->setDate(date('d',strtotime($shipDate)), date('m',strtotime($shipDate)), date('Y',strtotime($shipDate)));
$myCalendar->setPath('../calendar');
$myCalendar->setYearInterval(2010, 2020);
$myCalendar->dateAllow('2010-03-01', '2020-12-31');
//$myCalendar->setHeight(350);
//$myCalendar->autoSubmit(true, 'form1');
$myCalendar->writeScript();


echo "</td>
</tr>
<tr>

<td class='listtext' align='right'>Freight Cost</td>
<td><input name='shipCost' value='$shipCost' class='bodytext' size='13' /></td>

<td class='listtext' width='140' align='right'>Est. Delivery Date</td>
<form name='form2' method='get' action='../calendar/calendar_form.php'>
<td class='bodytext'>";

$myCalendar = new tc_calendar('deliveryDate', true, false);
$myCalendar->setIcon('../calendar/images/iconCalendar.gif');
$myCalendar->setDate(date('d',strtotime($deliveryDate)), date('m',strtotime($deliveryDate)), date('Y',strtotime($deliveryDate)));
$myCalendar->setPath('../calendar');
$myCalendar->setYearInterval(2010, 2020);
$myCalendar->dateAllow('2010-03-01', '2020-12-31');
//$myCalendar->setHeight(350);
//$myCalendar->autoSubmit(true, 'form2');
$myCalendar->writeScript();

echo "</td>
</form>";

Exec page:
$shipDate = isset($_REQUEST['shipDate']) ? $_REQUEST['shipDate'] : "";
$deliveryDate = isset($_REQUEST['deliveryDate']) ? $_REQUEST['deliveryDate'] : "";
 

Martin on 08 Mar 2010 07:20 reply

Great little tool! I like it very much!
Is there a simple way to change the calender language to something else than english?

However it is exactly what I was looking for and is very sleek and simple.

Martin on 08 Mar 2010 08:17 reply

Found the solution in the posts below! Sorry!

Only one more question about localization:
How do I localize the form-field with the set date? It remains english, whatever I do...
TJ on 09 Mar 2010 06:42 reply

I think you should edit the word in tc_calendar.php class function writeDay(), writeMonth(), writeYear() respectively :)
 

Mark on 03 Mar 2010 15:50 reply

Hi there,

Fantastic little script, works a treat, only problem is under IE6 some of the elements within my form appears on top of data picker when it appears, any idea why this might be?

 

Denise on 01 Mar 2010 11:19 reply

That's a very good script but I want to use without iframes because its deprecated. I've tried to use the php include but the values are not updated anymore :-( Any ideas?

 

shi on 26 Feb 2010 09:27 reply

How can I extend it by selecting the month field & get the days of the month ie. when february is selected then day have to be only 28 days. This script has no validator if i select 31 th february. Please help me ...

TJ on 09 Mar 2010 06:47 reply

umm that should be a missing thing, I will try to apply it soon :[
 

Roopak on 26 Feb 2010 03:05 reply

Thnx for this. Really appreciate your endeavor.

Roopak
http://nationalview.in

 

bon on 25 Feb 2010 01:00 reply

hi ! how can i get the day of week being selected? for example, Monday, Tues...etc...

If I select Feb 25, 2010 then its Thursday. How will I be able to get that value? Please help..Thanks a lot.

TJ on 25 Feb 2010 11:58 reply

after you retrieve the date value submitted please try date('l') <- small L

$date1 = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";

if($date1 && strtotime($date1)){
echo(date('l', strtotime($date1))); //print sunday through saturday refer from php date manual
}

alternative use date('N') for monday through sunday
 

Robert on 24 Feb 2010 06:00 reply

Can u plz tell me how to store two time
i.e From time: && To time::

and fine the difference between them?

Plz Help

TJ on 25 Feb 2010 11:50 reply

you should retrieve 2 dates after submit the form, for example

$date1 = isset($_REQUEST["date_from"]) ? $_REQUEST["date_from"] : "";

$date2 = isset($_REQUEST["date_to"]) ? $_REQUEST["date_to"] : "";

$date1_time = strtotime($date1);
$date2_time = strtotime($date2);

if($date1_time > $date2_time){
$dif_time = $date1_time-$date2_time;
}else{
$dif_time = $date2_time-$date1_time;
}

//make the different in second
$dif_time = $dif_time / (60*60);

then you can store $date1, $date2, $dif_time to the db
Mike on 05 Mar 2010 23:12 reply

I also have two dates and I have two problems.

1. If I click on the calendar icon, I can set the first date, but if I click on it at the side of the second date, it just repopulates the first date.

Secondly how do I get the dates associated with field names on my form? should I be using hidden input field (which I tried using a value= to the $myCalendar->writeString(); but it put the " /> on my form as well
Any help much appreciated.
 

Ian on 17 Feb 2010 19:16 reply

I don't know if I am doin anythin wrong but I put all the code provided into my webpage and nothing shows.

Does anyone know of a reason for this?


Thanks
Ian

 

txs on 17 Feb 2010 02:51 reply

GREAT SCRIPT

TXS

 

Tim on 16 Feb 2010 10:19 reply

Excellent, works well. I'm storing the results in a database, and if I re-open an item to edit, I set the date automatically using the content from the database. However, if the date is not changed, when you post the form again the dates are set to nothing. Any ideas?

 

Hein de Kock on 13 Feb 2010 02:50 reply

This is a beatiful peace of work. I am a beginner with PHP and it did not take me long to insert a profesional looking date time picker on a webpage.

I think there may be a problem with the isDateAllow function in the javascript. If I do not set 'dateAllow' all is fine but with it, any change on the picker results in an alert displaying a date that is totaly diffrent form the picker. I can see where the alerts are called from but the javascript is beyond me. The calender displays correct though and all out of range dates are not selectable.

Still a very nice peace of work. Thank you

 

alice on 09 Feb 2010 13:04 reply

Hello,
I want to add a button that creates a new calendar object every time. How can I do that?
Thank you very much!

TJ on 11 Feb 2010 23:03 reply

do you want it dynamic add calendar? this should do by using ajax or javascript. I never do that before but possible :)
 

Rich on 05 Feb 2010 09:04 reply

WOOHOO all my problems are fixed, took me some time but it's done!!

I have now successfully got 2 x "Date 3" calendars working on the same form and they also both submit their individual values to my MySQL Database!!

Contact me for details!

ADS on 11 Feb 2010 19:32 reply

How u did it ? Please if possible give me details, Thanks!

Jen on 09 Mar 2010 11:05 reply

I am also wondering how to execute with more than one calendar in the form - help :)
 

Rich on 05 Feb 2010 07:43 reply

Hi Guys,

Rich here again!!

I have yet another issue!! How do I get the date to be submitted into a mysql database along with the other information in my form????

I hope you can help this time too?!

Thanks

 

Danny on 03 Feb 2010 15:10 reply

Hi nice script thanks!

I have one query though:

How can I set the default of the first date to the value from a recordset? I have been trying various ways but it always adds the "0000-00" in front of the hidden inputs value as well as the full date from the recordset value.

The only way I seem to be able to pull the value from the recordset is by using the following:

$myCalendar->setDate($row_rsBookings['date_from']);


Any ideas on what I'm doing wrong?

TJ on 11 Feb 2010 23:07 reply

the setDate should have 3 parameters: day, month, year. It seem that you put all the date in one parameter and it is wrong :)
adamz on 08 Mar 2010 16:00 reply

well is there a way to create a custom function to allow the current date to be pulled off from a recordset?

im having this problem as well. what i want is when i allow the user to edit a date i would like to show the date.

if u can create a custom function for this it would be really appreciated.
 

Rich on 03 Feb 2010 08:25 reply

I love this script!!!

I am having a problem though.. I have installed the script but it is displaying "select" where the image file should be and then when you click on it, it displays "Object not found" instead of the calendar!!?

PLEASE HELP!!

TJ on 03 Feb 2010 10:56 reply

it is about the path thing, please check setPath() and setIcon() function

use relative path from your script.
Rich on 04 Feb 2010 05:11 reply

Magic, thanks for the help TJ, my calendar works fine now!

Woohoo!!

Did I mention that this script is amazing?
 

jlo on 03 Feb 2010 05:50 reply

First of all, thanks for the code.

Second.. i have a question..

How can i get seperate values from next page. For example i want to get the day, month and year and put them in 3 seperate variables?

TJ on 03 Feb 2010 10:53 reply

you can try this after receive the date value

$date1 = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";

//date1 should be in format YYYY-MM-DD

list($year, $month, $day) = explode("-", $date1, 3);

then you can use 3 variables above :)
jlo on 05 Feb 2010 09:08 reply

Thanks men. Its amazing that you are replying on each comment here.

Ed on 08 Feb 2010 12:19 reply

I am woefully new to PHP. How do I launch another php page and send the selected date to that page?
 

Syamuri on 02 Feb 2010 01:23 reply

Hi, I am loving your tool
Thanks

 

M@tterhorn on 31 Jan 2010 09:30 reply

Nice script! Thanks also from Switzerland!

 

Enrique on 29 Jan 2010 02:57 reply

First of all, great work on the datepicker.

My problem is basically focused on the directories. I want to create a calendar folder. This folder contains all extracted files from calendar.zip.

I managed to change all the directories to:

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

<body>

</body>
<form action="somewhere.php" method="post">
<?php
//get class into the page
require_once('calendar/classes/tc_calendar.php');

//instantiate class and set properties
$myCalendar = new tc_calendar("date1", true);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(1, 1, 2000);

//output the calendar
$myCalendar->writeScript();
?>
</form>

</html>

The problem with this is the page displays the example above "Date 2" but when i click the calendar icon it cannot display the calendar. It just gives me Object not found....

I could sure use a little enlightening.... Thanks.

M@tterhorn on 31 Jan 2010 09:39 reply

If you use the index.php from the directory "calendar" the path must be like them :

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

<body>

</body>
<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>

</html>

Only when you use the index.php from a other directory or one level higher you have to change the path.
 

John on 27 Jan 2010 07:10 reply

Hi thank you so much for the code it is exactly what I needed.
I am just wondering what is the simplest way to go about validating the dates making sure that the user picked a date.

Thanks in advance

 

Maurice on 26 Jan 2010 15:25 reply

This is really one of the best calendar scripts I have ever seen! My compliments!!

There is only one thing I am strugling with;
I want to use the date pair example
How can I retrieve the separate dates?
I am only retrieving the date1 and not the date2 which I have set.

Thanks for your reply.
Regards,
Maurice

Maurice on 26 Jan 2010 16:19 reply

I have found the error I have made.... I used to FORMs instead of one.

Thanks for the script!
 

radox on 26 Jan 2010 08:55 reply

Thank you very much, this is great, very useful i had to change some year limits to allow the selection of birth years and the code was really easy to follow
great work

 

Kittu on 25 Jan 2010 06:50 reply

Excellent Sript

i have a small doubt on
i want the format in this way

$callendar->dateAllow('some date', 'today');

i tried in different way, but unable to get it

plz help me out.

 

DjPic on 12 Jan 2010 12:05 reply

(4 because there is always room for improvement)

How do you get the calendar to work without changing your page's font type and size? If I don't include the stylesheet on the page it is being embeded on, then the calendar's appearance goes out of wack.

Also, the close button is not appearing on mine. I moved it to a different location but can't find where to change it. Anyone know?

DjPic on 12 Jan 2010 12:27 reply

I found it....move the "body" and "form" style's to calendar_form.php file.
 

Mike on 11 Jan 2010 18:38 reply

Using date 2 with drop down boxes for M, D and Y. Can I get the current date already populated in the form? New to php but can do a little stuff. Having problem getting current date. Thanks for great script.

DjPic on 12 Jan 2010 12:01 reply

$Today['d'] = date('d');
$Today['m'] = date('m');
$Today['y'] = date('Y');
$myCalendar->setDate($Today['d'], $Today['m'], $Today['y']);
 

Dugi on 11 Jan 2010 10:49 reply

Hi! I want to use this date picker in php script and not html file. Could you please show me any sample for it?

 

Fia on 04 Jan 2010 06:43 reply

Found it row 226,212, 201 in tc_calander.php

 

Fia on 04 Jan 2010 06:31 reply

Wonderful script, Is there a way to translate det day,month and year text in the selectionboxes. I've found the arrays for days and month for the form but couldn't finde where to translate the selectionboxes.

Lots of thanx

 
See all reviews 1 2 3 4 5 6 7 Next»
Write a review
Rating:
Text:
Name:
Email:
(optional)
 
Code:
 

Search this site