PHP Date Picker

This is a simple to use script that can be called at anytime to insert a date picker into your form.

Example


Usage

Just make a call to

echo DatePicker();

wherever you want the date picker to show. If you want to use it multiple times, just call it multiple times like

echo DatePicker();
echo DatePicker(); 

but make sure you have the first line of the code un commented.

The Code

$iDatePickerCounter = '';		// Used for having multiple date pickers
								// comment it out if you are only going to
								// use ONE datepicker call on this page.
								// (It will make the form element names
								// nicer to work with).
$sYearName 			= 'Year';	// Base name for the Year form element
$sMonthName 		= 'Month';	// Base name for the Month form element
$sDayName 			= 'Day';	// Base name for the Day form element

$iFromYear			= 1985;		// Starting Year
$iToYear			= 2030;		// Ending Year

function DatePicker(){
	// Call up the global variables
	global $iDatePickerCounter,
		   $sYearName,
		   $sMonthName,
		   $sDayName,
		   $iFromYear,
		   $iToYear;

	// Set up some base variables
	$sPostFix = '';
	$sNL = "\r\n";

	if(isset($iDatePickerCounter)){
		$iDatePickerCounter++;
		$sPostFix = '_' . $iDatePickerCounter;
	}

	// Start the coding of the SELECT areas
	$sYearDropDown 	= '
' . $sNL;
	$sDayDropDown 	= '


' . $sNL . $sNL;

	// Month Loop
	$sDummyDate = '2008-01-01';
	for($i = 0; $i < 12; $i++){
		$sMonthDropDown .= "\t" .'
Posted in PHP by Ed at January 16th, 2009.

You Might Also Like...

6 Responses to “PHP Date Picker”

  1. Ohana says:

    easy and simple…ty for script

    • peter says:

      your script has a problem. Its nice i would like to use it but has characters which are not meant to be there but are there and they cause errors .

      Just try it like any other person by copying the code you posted on the site here and see what we are experiencing.

      The problem could be that you did not post it well. please check it out coz it good and we want to use it.

      Kind Regards

  2. Ed says:

    Hi Peter,

    Thanks for pointing this out – it should be fixed now!

    Ed.

  3. Faisal says:

    <option value="' . $i . '" ';="" $syeardropdown="" .="(isset($_REQUEST[$sYearName" $spostfix]))="" ?="" (($_request[$syearname="" $spostfix]="=" $i)="" '="" selected="selected" :="" '')="" '';="" $i="" '

    the above code give error in php designer

    could you check please

  4. Terry says:

    It looks like the error is still in the posted version but if you just right click the web page and select “View Page Source” (or equivalent) then you can copy and paste the source for the working version.

Leave a Reply