开发者

PHP convert month-number to short month-name [duplicate]

开发者 https://www.devze.com 2023-02-02 12:40 出处:网络
This question already has answers here: Convert month number to month short name (6 answers) 开发者_JAVA百科
This question already has answers here: Convert month number to month short name (6 answers) 开发者_JAVA百科 Convert number to month name in PHP (22 answers) Closed 8 years ago.

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.


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');


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));
0

精彩评论

暂无评论...
验证码 换一张
取 消