PHP Month year day selection drop down list to generate date format
In different forms we will be asking visitors to enter the date in an input field in a particular format. The better way is to ask the visitors to select month, date and year from a drop down list box. So the visitor can easily make the choice of date month and year. The drop down list box will contain only the valid items so there is no need to check the inputs again. We will try to create two drop down box for month and date and one text box for entering year. You can change the year text entry box to drop down list box with your options of years.
We will use one form and inside that we will add all three form components to accept month date and year from the visitors. Using the three values entered by users we can create a date format which we can use in and SQL query to insert to any database or store. The format yyyy-mm-dd is used to store in a date field of mysql table.
Please note that here the form is submitted to the same page and the variables are available for our use. If in your server settings global variables are set for OFF then you have to collect the form submitted values as $month= $_POST['month'] or in different ways based on the version of PHP you are running. Here is the demo of the drop down list box for date format.
The code for the above list boxes and the form is here.
<?Php
$todo=$_POST['todo'];
if(isset($todo) and $todo=="submit"){
$month=$_POST['month'];
$dt=$_POST['dt'];
$year=$_POST['year'];
$date_value="$month/$dt/$year";
echo "mm/dd/yyyy format :$date_value<br>";
$date_value="$year-$month-$dt";
echo "YYYY-mm-dd format :$date_value<br>";
}
for($i=0;$i<=11;$i++){
$month=date('M',strtotime("first day of -$i month"));
echo $month ."<br>";
}
Output is here
Mar Feb Jan Dec Nov Oct Sep Aug Jul Jun May Apr
Creating a dropdown list using above code
echo "<select name=month>";
for($i=0;$i<=11;$i++){
$month=date('F',strtotime("first day of -$i month"));
echo "<option value=$month>$month</option> ";
}
echo "</select>";
Output is here
Listing next 5 years starting from present year
echo "<select name=year>";
for($i=0;$i<=5;$i++){
$year=date('Y',strtotime("last day of +$i year"));
echo "<option name='$year'>$year</option>";
}
echo "</select>";
Output is here
Using JQuery Calendar Widget
By adding JQuery we can develop calendar easily and allow users to select data from it. We can enable or disable range of data and provide interlocking between different calendars. JQuery code is easy to maintain and implement.