A wonderful Calendar for picking date in PHP Input/Update forms

I was writing PHP scripts the other day where I needed a calendar to pick dates. My user needs to pick date of any month or year. Problem was — Manual input of dates are tiresome and inefficient. On the other hand, if inputs are in string, I need to convert them to “datetime” as my datatype is such in the MySQL DB.

I found a wonderful Calendar a website. He made it in javascript and works very well. There are several layouts in the calendar from which developers can choose anyone. The more important thing is, you just need to pass the PHP variable which will store into the DB. You can have this Javascript Calendar from downloading from this link .

This code segment is for taking input and inserting into database

/*include this section*/
<?php require_once('calendar/classes/tc_calendar.php'); ?>

<!-- calender js link-->
<script language="javascript" src="calendar/calendar.js"></script>
<!-- finished-->
<?php
	  $myCalendar = new tc_calendar("date5", true, false); //date5 is similar to the column name of DB 
	  $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->setAlignment('left', 'bottom');
	  $myCalendar->setSpecificDate(array("2011-04-01", "2011-04-04", "2011-12-25"), 0, 'year');
	  $myCalendar->setSpecificDate(array("2011-04-10", "2011-04-14"), 0, 'month');
	  $myCalendar->setSpecificDate(array("2011-06-01"), 0, '');
	  $myCalendar->writeScript();
	  ?>

This code segment is for Updating date into database
You need to bring out a variable ($BIRTHDAY in this example) that is used to extract data from DB. like, if you use and array like mysql_fetch_array($birthday) to extract data from DB. you can use $BIRTHDAY = mysql_fetch_array($birthday). The code segment will make clear everything I believe.

/*include this section*/
<?php require_once('calendar/classes/tc_calendar.php'); ?>

<!-- calender js link-->
<script language="javascript" src="calendar/calendar.js"></script>
<!-- finished-->
<?php
	 $myCalendar = new tc_calendar("date1", true, true);
         $myCalendar->zindex = 150; //default 1
         $myCalendar->setIcon("calendar/images/iconCalendar.gif");
         $myCalendar->setPath("calendar/");
         if(isset($BIRTHDAY))
         /*this portion works to show DB date to form*/
         {
         $birth_day = strtotime($BIRTHDAY); //$BIRTHDAY is the variable extracted from DB
         $myCalendar->setDate(date('d',$birth_day), date('m',$birth_day), date('Y',$birth_day));
         }
         /*tricky portion ends here*/
         $myCalendar->setYearInterval(1935, date('Y'));
         $myCalendar->dateAllow('1935-01-01', date('Y-m-d'));
         $myCalendar->writeScript();
?>

4 thoughts on “A wonderful Calendar for picking date in PHP Input/Update forms

  1. Hello,

    Which variable is actually saving the date?? in your first code.. ? i mean which variable should be passed?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s