Today we can Learn How to Create Reusable
Date picker Using
Php Programming Language.Its reusable php function for datepicker.Just Apply the function and get the datepicker.
Just follow this step for creating a Datepicke:
Then we’ll construct the three dropdowns like month ,day,year
:
For Month :
$html="<select name=\"".$name."month\">";
for($i=1;$i<=12;$i++)
{
$html.="<option value='$i'>$months[$i]</option>";
}
$html.="</select> ";
For Day :
$html.="<select name=\"".$name."day\">";
for($i=1;$i<=31;$i++)
{
$html.="<option value='$i'>$i</option>";
}
$html.="</select> ";
For Year :
$startyear = date("Y")-100;
$endyear= date("Y")+50;
$html.="<select name=\"".$name."year\">";
for($i=$startyear;$i<=$endyear;$i++)
{
$chooser.="<option value='$i'>$i</option>";
}
$html.="</select> ";
Final code for datepickr :
function date_picker($name, $startyear=NULL, $endyear=NULL)
{
if($startyear==NULL) $startyear = date("Y")-100;
if($endyear==NULL) $endyear=date("Y")+50;
$months=array('','January','February','March','April','May',
'June','July','August', 'September','October','November','December');
// Month dropdown
$html="<select name=\"".$name."month\">";
for($i=1;$i<=12;$i++)
{
$html.="<option value='$i'>$months[$i]</option>";
}
$html.="</select> ";
// Day dropdown
$html.="<select name=\"".$name."day\">";
for($i=1;$i<=31;$i++)
{
$html.="<option $selected value='$i'>$i</option>";
}
$html.="</select> ";
// Year dropdown
$html.="<select name=\"".$name."year\">";
for($i=$startyear;$i<=$endyear;$i++)
{
$html.="<option value='$i'>$i</option>";
}
$html.="</select> ";
return $html;
}
echo date_picker("registration");
use the function as echo date_picker(“registration”) (for example – “registration” is just a name you choose).
The result that will come in $_POST after submitting such form
$_POST['registrationmonth'], $_POST['registrationday'] and
$_POST['registrationyear'].
Display Preview:
