This is a simple to use script that can be called at anytime to insert a date picker into your form.
Just make a call to
wherever you want the date picker to show. If you want to use it multiple times, just call it multiple times like
but make sure you have the first line of the code un commented.
$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 we're using the counter (for more than one
// date picker), increment the counter and create
// the postfix string.
if(isset($iDatePickerCounter)){
$iDatePickerCounter++;
$sPostFix = '_' . $iDatePickerCounter;
}
// Start the coding of the SELECT areas
$sYearDropDown = '<select name="' . $sYearName . $sPostFix . '" id="' . $sYearName . $sPostFix . '">' . $sNL;
$sMonthDropDown = '<select name="' . $sMonthName . $sPostFix . '" id="' . $sMonthName . $sPostFix . '">' . $sNL;
$sDayDropDown = '<select name="' . $sDayName . $sPostFix . '" id="' . $sDayName . $sPostFix . '">' . $sNL;
// Year loop
for($i = $iFromYear; $i <= $iToYear; $i++){
$sYearDropDown .= "\t" . '<option value="' . $i . '"';
// Here we're checking if a POST or GET variable with
// the same name as this SELECT element is set. If it
// is, then we're going to see if the values of the
// SELECT element (in this iteration) matches the
// value of the request element. If it does, well set
// this option as selected by default.
$sYearDropDown .= (isset($_REQUEST[$sYearName . $sPostFix])) ?
(($_REQUEST[$sYearName . $sPostFix] == $i) ? ' selected="selected"' : ") :
";
$sYearDropDown .= '>' . $i . '</option>' . $sNL;
}
$sYearDropDown .= '</select>' . $sNL . $sNL;
// Month Loop
$sDummyDate = '2008-01-01';
for($i = 0; $i < 12; $i++){
$sMonthDropDown .= "\t" .'<option value="' .
date('m', strtotime('+' . $i . ' months', strtotime($sDummyDate))) . '"';
// Here we're checking if a POST or GET variable with
// the same name as this SELECT element is set. If it
// is, then we're going to see if the values of the
// SELECT element (in this iteration) matches the
// value of the request element. If it does, well set
// this option as selected by default.
$sMonthDropDown .= (isset($_REQUEST[$sMonthName . $sPostFix])) ?
(($_REQUEST[$sMonthName . $sPostFix] == date('m', strtotime('+' . $i . ' months', strtotime($sDummyDate)))) ?
' selected="selected"' : ") : ";
$sMonthDropDown .= '>' . date('M', strtotime('+' . $i . ' months', strtotime($sDummyDate))) . '</option>' . $sNL;
}
$sMonthDropDown .= '</select>' . $sNL . $sNL;
// Day loop
for($i = 1; $i <= 31; $i++){
$sDayDropDown .= "\t" .'<option value="' . $i . '"';
// Here we're checking if a POST or GET variable with
// the same name as this SELECT element is set. If it
// is, then we're going to see if the values of the
// SELECT element (in this iteration) matches the
// value of the request element. If it does, well set
// this option as selected by default.
$sDayDropDown .= (isset($_REQUEST[$sDayName . $sPostFix])) ?
(($_REQUEST[$sDayName . $sPostFix] == $i) ? ' selected="selected"' : ") :
";
$sDayDropDown .= '>' . $i . '</option>' . $sNL;
}
$sDayDropDown .= '</select>' . $sNL . $sNL;
// Return all three dropdowns
return $sYearDropDown . $sMonthDropDown . $sDayDropDown;
}
Discussion
No comments for “PHP Date Picker”
Post a comment