Convert number to month name in PHP

2019-01-04 18:35发布

I have this PHP code:

$monthNum = sprintf("%02s", $result["month"]);
$monthName = date("F", strtotime($monthNum));

echo $monthName;

But it's returning December rather than August.

$result["month"] is equal to 8, so the sprintf function is adding a 0 to make it 08.

标签: php date
18条回答
Root(大扎)
2楼-- · 2019-01-04 19:08

Am currently using the solution below to tackle the same issue:

//set locale, 
setlocale(LC_ALL,"US");

//set the date to be converted
$date = '2016-08-07';

//convert date to month name
$month_name =  ucfirst(strftime("%B", strtotime($date)));

echo $month_name;

To read more about set locale go to http://php.net/manual/en/function.setlocale.php

To learn more about strftime go to http://php.net/manual/en/function.strftime.php

Ucfirst() is used to capitalize the first letter in a string.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-04 19:10

To do the conversion in respect of the current locale, you can use the strftime function:

setlocale(LC_TIME, 'fr_FR.UTF-8');                                              
$monthName = strftime('%B', mktime(0, 0, 0, $monthNumber));

date doesn't respect the locale, strftime does.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-04 19:12

Just because everyone is using strtotime() and date() functions, I will show DateTime example:

$dt = DateTime::createFromFormat('!m', $result['month']);
echo $dt->format('F');
查看更多
Deceive 欺骗
5楼-- · 2019-01-04 19:12

strtotime expects a standard date format, and passes back a timestamp.

You seem to be passing strtotime a single digit to output a date format from.

You should be using mktime which takes the date elements as parameters.

Your full code:

$monthNum = sprintf("%02s", $result["month"]);
$monthName = date("F", mktime(null, null, null, $monthNum));

echo $monthName;

However, the mktime function does not require a leading zero to the month number, so the first line is completely unnecessary, and $result["month"] can be passed straight into the function.

This can then all be combined into a single line, echoing the date inline.

Your refactored code:

echo date("F", mktime(null, null, null, $result["month"], 1));

...

查看更多
Evening l夕情丶
6楼-- · 2019-01-04 19:13

The recommended way to do this:

Nowadays, you should really be using DateTime objects for any date/time math. This requires you to have a PHP version >= 5.2. As shown in Glavić's answer, you can use the following:

$monthNum  = 3;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F'); // March

The ! formatting character is used to reset everything to the Unix epoch. The m format character is the numeric representation of a month, with leading zeroes.

Alternative solution:

If you're using an older PHP version and can't upgrade at the moment, you could this solution. The second parameter of date() function accepts a timestamp, and you could use mktime() to create one, like so:

$monthNum  = 3;
$monthName = date('F', mktime(0, 0, 0, $monthNum, 10)); // March

If you want the 3-letter month name like Mar, change F to M. The list of all available formatting options can be found in the PHP manual documentation.

查看更多
Luminary・发光体
7楼-- · 2019-01-04 19:16

Use a native function such as jdmonthname():

echo jdmonthname($monthNum, CAL_MONTH_GREGORIAN_LONG);
查看更多
登录 后发表回答