PHP Month Year Day Selection Drop Down List To Generate Date Format

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PHPMonthyeardayselectiondropdownlisttogeneratedateformat

Indifferentformswewillbeaskingvisitorstoenterthedateinaninputfieldinaparticularformat.The
betterwayistoaskthevisitorstoselectmonth,dateandyearfromadropdownlistbox.Sothevisitorcan
easilymakethechoiceofdatemonthandyear.Thedropdownlistboxwillcontainonlythevaliditemsso
thereisnoneedtochecktheinputsagain.Wewilltrytocreatetwodropdownboxformonthanddateand
onetextboxforenteringyear.Youcanchangetheyeartextentryboxtodropdownlistboxwithyour
optionsofyears.

DateValidation
Notethatwearenotvalidatinganyinputshereandyoucanaddthemifrequired.Youcancheck
differentvalidationsusingPHPhere.

RELATEDTUTORIALSValidatinguserenteredDateCalendarforuserselection
Wewilluseoneformandinsidethatwewilladdallthreeformcomponentstoacceptmonthdateandyear
fromthevisitors.Usingthethreevaluesenteredbyuserswecancreateadateformatwhichwecanusein
andSQLquerytoinserttoanydatabaseorstore.Theformatyyyymmddisusedtostoreinadatefieldof
mysqltable.

Pleasenotethatheretheformissubmittedtothesamepageandthevariablesareavailableforouruse.Ifin
yourserversettingsglobalvariablesaresetforOFFthenyouhavetocollecttheformsubmittedvaluesas
$month=$_POST['month']orindifferentwaysbasedontheversionofPHPyouarerunning.Hereisthe
demoofthedropdownlistboxfordateformat.

Thecodefortheabovelistboxesandtheformishere.

<?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/yyyyformat:$date_value<br>";

$date_value="$year$month$dt";

echo"YYYYmmddformat:$date_value<br>";

?>

<formmethod=postname=f1action=''><inputtype=hiddenname=todovalue=submit>

<tableborder="0"cellspacing="0">

<tr><tdalign=left>

<selectname=monthvalue=''>SelectMonth</option>

<optionvalue='01'>January</option>

<optionvalue='02'>February</option>

<optionvalue='03'>March</option>

<optionvalue='04'>April</option>

<optionvalue='05'>May</option>

<optionvalue='06'>June</option>

<optionvalue='07'>July</option>

<optionvalue='08'>August</option>

<optionvalue='09'>September</option>

<optionvalue='10'>October</option>

<optionvalue='11'>November</option>

<optionvalue='12'>December</option>

</select>

</td><tdalign=left>

Date<selectname=dt>

<optionvalue='01'>01</option>

<optionvalue='02'>02</option>

<optionvalue='03'>03</option>

<optionvalue='04'>04</option>

<optionvalue='05'>05</option>

<optionvalue='06'>06</option>

<optionvalue='07'>07</option>

<optionvalue='08'>08</option>

<optionvalue='09'>09</option>

<optionvalue='10'>10</option>

<optionvalue='11'>11</option>

<optionvalue='12'>12</option>

<optionvalue='13'>13</option>

<optionvalue='14'>14</option>

<optionvalue='15'>15</option>

<optionvalue='16'>16</option>

<optionvalue='17'>17</option>

<optionvalue='18'>18</option>

<optionvalue='19'>19</option>

<optionvalue='20'>20</option>

<optionvalue='21'>21</option>

<optionvalue='22'>22</option>

<optionvalue='23'>23</option>

<optionvalue='24'>24</option>

<optionvalue='25'>25</option>

<optionvalue='26'>26</option>

<optionvalue='27'>27</option>

<optionvalue='28'>28</option>

<optionvalue='29'>29</option>

<optionvalue='30'>30</option>

<optionvalue='31'>31</option>

</select>

</td><tdalign=left>

Year(yyyy)<inputtype=textname=yearsize=4value=2005>

<inputtype=submitvalue=Submit>

</table>

</form>

ListingallpreviousMonthsstartingfromcurrentmonthusingstrtotime()

Strtotimereturnsunixtimestampbytakingtextstringofdate&timeinputs

Hereisthecode

for($i=0;$i<=11;$i++){

$month=date('M',strtotime("firstdayof$imonth"));

echo$month."<br>";

Outputishere

Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
Dec
Nov
Oct
Sep

Creatingadropdownlistusingabovecode

echo"<selectname=month>";

for($i=0;$i<=11;$i++){

$month=date('F',strtotime("firstdayof$imonth"));

echo"<optionvalue=$month>$month</option>";

echo"</select>";

Outputishere

Listingnext5yearsstartingfrompresentyear

echo"<selectname=year>";

for($i=0;$i<=5;$i++){

$year=date('Y',strtotime("lastdayof+$iyear"));

echo"<optionname='$year'>$year</option>";

echo"</select>";

Outputishere

You might also like