4

I need to convert month-number to short month-name (i.e., 1 for Jan, 2 for Feb)

I know that I can achieve this via Array, but is there any other way to do it?

Help appreciated.

Thanks.

I-M-JM
  • 15,732
  • 26
  • 77
  • 103

2 Answers2

14

Yes there is. Use date/stftime in combination with mktime to create a timestamp within the desired month.

Strftime is cool because it will read the locale setting, and output your written date parts in that specific language.

For example:

$time = mktime(0, 0, 0, $monthNumber);
$name = strftime("%b", $time);

Now lets say you want your short month names in german language you call setlocale before calling strftime:

setlocale(LC_TIME, 'de_DE');
fresskoma
  • 25,481
  • 10
  • 85
  • 128
4
for ($i=1;$i<=12;$i++) {
  echo date ("M", mktime(0,0,0,$i,1,0))."<br />";
} 

Without the for loop, for month 6:

  echo date ("M", mktime(0,0,0,6,1,0));
Ben
  • 54,723
  • 49
  • 178
  • 224