dc_jt Posted May 17, 2007 Share Posted May 17, 2007 Hi I have the following code which is a drop down containing a month and a year. At the moment, the months are displayed 1,2,3,4 etc, however I want them to be displayed as Jan, Feb, Mar etc but the values to remain 01, 02, 03 etc. <form name="date" action="<?=$_SERVER['PHP_SELF']?>?sMode=ChangeFilter" method="POST" class="sort"> <label>Date:</label> <select name="datem" id="datem"> <?php $iCurMonth = ('5'); for ($i=1;$i<=12;$i++) { ?> <option <?=((isset($aFilterParts['datem']) && $i==$aFilterParts['datem']) || (!isset($aFilterParts['datem']) && $i==$iCurMonth))?' selected="selected"':''?>><?=$i?></option> <?php } ?> </select> <select name="datey" id="datey"> <?php $iCurYear = ('2007'); for ($i=2007;$i<=2009;$i++) { ?> <option <?=((isset($aFilterParts['datey']) && $i==$aFilterParts['datey']) || (!isset($aFilterParts['datey']) && $i==$iCurYear))?' selected="selected"':''?>><?=$i?></option> <?php } ?> </select> <input type="Submit" name="submit" value="Submit"/> </form> Thanks Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/ Share on other sites More sharing options...
kenrbnsn Posted May 17, 2007 Share Posted May 17, 2007 Create an array to hold the names of the months and use $i to index to the correct one. <?php $mnths = array('January','February','March','April','May','June','July','August','September','October','November','December'); $iCurMonth = ('5'); for ($i=1;$i<=12;$i++) { $sel = ((isset($aFilterParts['datem']) && $i==$aFilterParts['datem']) || (!isset($aFilterParts['datem']) && $i==$iCurMonth))?' selected="selected"':''; echo '<option ' . $sel . ' value=' . $i . '>' . $mnths[$i] . '</option>'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255464 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 or just use this :-) <?php function switch_month($m){ while($m{0}=="0") $m=substr($m,1); $m=strtolower($m); switch($m){ case "1": return "january"; break; case "2": return "february"; break; case "3": return "march"; break; case "4": return "april"; break; case "5": return "may"; break; case "6": return "june"; break; case "7": return "july"; break; case "8": return "august"; break; case "9": return "september"; break; case "10": return "october"; break; case "11": return "november"; break; case "12": return "december"; break; case "january": return "1"; break; case "february": return "2"; break; case "march": return "3"; break; case "april": return "4"; break; case "may": return "5"; break; case "june": return "6"; break; case "july": return "7"; break; case "august": return "8"; break; case "september": return "9"; break; case "october": return "10"; break; case "november": return "11"; break; case "december": return "12"; break; } } #and on yourloop <?=switch_month($i)?> ?> Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255472 Share on other sites More sharing options...
dc_jt Posted May 17, 2007 Author Share Posted May 17, 2007 Thanks a lot, this worked, however January is not included for some reason?? Hi I have the following code which is a drop down containing a month and a year. At the moment, the months are displayed 1,2,3,4 etc, however I want them to be displayed as Jan, Feb, Mar etc but the values to remain 01, 02, 03 etc. <form name="date" action="<?=$_SERVER['PHP_SELF']?>?sMode=ChangeFilter" method="POST" class="sort"> <label>Date:</label> <select name="datem" id="datem"> <?php $iCurMonth = ('5'); for ($i=1;$i<=12;$i++) { ?> <option <?=((isset($aFilterParts['datem']) && $i==$aFilterParts['datem']) || (!isset($aFilterParts['datem']) && $i==$iCurMonth))?' selected="selected"':''?>><?=$i?></option> <?php } ?> </select> <select name="datey" id="datey"> <?php $iCurYear = ('2007'); for ($i=2007;$i<=2009;$i++) { ?> <option <?=((isset($aFilterParts['datey']) && $i==$aFilterParts['datey']) || (!isset($aFilterParts['datey']) && $i==$iCurYear))?' selected="selected"':''?>><?=$i?></option> <?php } ?> </select> <input type="Submit" name="submit" value="Submit"/> </form> Thanks Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255477 Share on other sites More sharing options...
kenrbnsn Posted May 17, 2007 Share Posted May 17, 2007 My mistake, add a null for the first value of the array: <?php $mnths = array('','January','February','March','April','May','June','July','August','September','October','November','December'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255479 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 yup... because without '', $i=1=febuary... Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255483 Share on other sites More sharing options...
dc_jt Posted May 17, 2007 Author Share Posted May 17, 2007 Thanks a lot Ken, great help. One last question. How do I set the $iCurMonth to the current month rather than 5, because obviously next month wont be 5? Thanks again Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255484 Share on other sites More sharing options...
dc_jt Posted May 17, 2007 Author Share Posted May 17, 2007 Does $iCurMonth = getdate("mon"); do it? Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255486 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 change it to date(m), so it automatically highlights this month :-) Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255488 Share on other sites More sharing options...
kenrbnsn Posted May 17, 2007 Share Posted May 17, 2007 You can use the date() function: <?php $iCurMonth = date('m'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255489 Share on other sites More sharing options...
dc_jt Posted May 17, 2007 Author Share Posted May 17, 2007 Thanks Ken and taith for your help Link to comment https://forums.phpfreaks.com/topic/51839-solved-changing-months-from-numbers-to-names/#findComment-255492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.