Any simple(ish) way to convert a 2-digit number (between 01 and 12) to the appropriate 3-letter month abbreviation (Jan, Feb, Mar, etc.)? I'm not pulling this out of a date stamp or anything, just starting with a raw number.

Thanks!

    You could put them in an array. eg.

    $date[] = "";
    $date[] = "Jan";
    $date[] = "feb";
    $date[] = "Mar";
    etc...

    then use:

    $day = $date[02];

    would equal 'Feb'.

    Damien.

      I'm trying to be lazier - like using some handy PHP function that might say

      three_letter_month($i) - where $i is the 2-digit number.

      Just wondering if one of the date functions will do this.

      But I came up with this (crude but effective) option:

      $j = 2492000;
      echo "<select name=\"whatever\">";
      for ($i=1; $i <= 12; $i++) {
      echo "<option value=\"" . sprintf("%02d", $i) . "\"";
      echo ">" . date("M", $j) . "</option>";
      $j=$j+2592000;
      }
      echo "</select>";

      which gives me a popup menu with the twelve months in it, and transmits 01-12. The $j variable adds 30 days each time through the loop, so as long as the initial value for $j is somewhere in January it seems to work.

        I thought my way was lazy enough. But I know what you mean. I did check the date() function to see if that could be done. I don't think there is an easy way out.

        Damien.

          Damien Butler's is probably the laziest. doing it with one statement:

          $months = array(
           '01'=>'Jan',
           '02'=>'Feb',
          ...
           '12'=>'Dec',
          );
          

          To make the dropdown with this array:

          foreach($months as $number=>$word)
          { echo "<option value='$number'>$word</option>";
          }
          

            You could use the mktime function to do what you want.

            for ($i=1;$i<=12;$i++) {
            $three_letter = date ("M", mktime(0,0,0,$i,1,2003));
            echo $three_letter . "<br>";
            }

              I still don't see how that could be said to be "lazier" than just writing the array - why call entire date/time functions a dozen times every time you want to display something that is pretty much fixed? It's more work for the server and more complicated to write.

                7 days later

                Thanks Weedpacket - your method works great.

                I needed to do it as a loop, and on each trip through the loop compare the numerical value of the month to a session variable, and if they matched echo "selected". And this did it!

                  Write a Reply...