Jump to content

[SOLVED] Changing Months from numbers to names


dc_jt

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.