SQL PHP HTML ASP JavaScript articles and free scripts to download

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.

Date Validation

Note that we are not validating any inputs here and you can add them if required. You can check different validations using PHP here.

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.

Date Year(yyyy)

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>";
}

?>
<form method=post name=f1 action=''><input type=hidden name=todo value=submit>
<table border="0" cellspacing="0" >
<tr><td  align=left  >   
<select name=month value=''>Select Month</option>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>

</td><td  align=left  >   
Date<select name=dt >
<option value='01'>01</option>


<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>


</td><td  align=left  >   
Year(yyyy)<input type=text name=year size=4 value=2005>
<input type=submit value=Submit>
</table>


</form>

Google+
Click for more tutorial on Date & Time functions and Applications


More On Date Object
vijayshankar07-08-2012
This was the best post thank a lot dude
muzika05-10-2013
not good. there shouldn't be february 30 and 31 and 29 should only appear when it is leap year.
smo06-10-2013
Date validation is added in next section. Here it is demonstrated how to collect the date inputs.
kossi14-02-2014
great! But can you help me on how to do the validation for the month february
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer