在 PHP 中将数字转换为月份名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18467669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert number to month name in PHP
提问by user2710234
I have this PHP code:
我有这个 PHP 代码:
$monthNum = sprintf("%02s", $result["month"]);
$monthName = date("F", strtotime($monthNum));
echo $monthName;
But it's returning Decemberrather than August.
但它正在返回December而不是August.
$result["month"]is equal to 8, so the sprintffunction is adding a 0to make it 08.
$result["month"]等于 8,因此sprintf函数添加 a0使其成为08。
回答by Amal Murali
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:
如今,您真的应该将 DateTime 对象用于任何日期/时间数学运算。这要求您的 PHP 版本 >= 5.2。如Glavi? 的回答所示,您可以使用以下内容:
$monthNum = 3;
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F'); // March
The !formatting characteris used to reset everything to the Unix epoch. The mformat character is the numeric representation of a month, with leading zeroes.
该!格式化字符用于重置一切的Unix纪元。该m格式字符是一月份的数字表示,用前导零。
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:
如果您使用的是较旧的 PHP 版本并且目前无法升级,则可以使用此解决方案。date()函数的第二个参数接受一个时间戳,您可以使用它mktime()来创建一个,如下所示:
$monthNum = 3;
$monthName = date('F', mktime(0, 0, 0, $monthNum, 10)); // March
If you want the 3-letter month name like Mar, change Fto M. The list of all available formatting options can be found in the PHP manual documentation.
如果您想要 3 个字母的月份名称Mar,请更改F为M。所有可用格式选项的列表可以在PHP 手册文档中找到。
回答by Glavi?
回答by mintedsky
Use mktime():
使用mktime():
<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; // Output: May
?>
See the PHP manual : http://php.net/mktime
请参阅 PHP 手册:http: //php.net/mktime
回答by Greg
strtotimeexpects a standard date format, and passes back a timestamp.
strtotime需要标准日期格式,并传回时间戳。
You seem to be passing strtotimea single digit to output a date format from.
您似乎正在传递strtotime一个数字来输出日期格式。
You should be using mktimewhich takes the date elements as parameters.
您应该使用mktimewhich 将日期元素作为参数。
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.
但是,mktime 函数不需要月份数字前导零,因此第一行完全不需要,$result["month"]可以直接传递到函数中。
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));
...
...
回答by Dereckson
To do the conversion in respect of the current locale, you can use the strftimefunction:
要根据当前语言环境进行转换,您可以使用以下strftime函数:
setlocale(LC_TIME, 'fr_FR.UTF-8');
$monthName = strftime('%B', mktime(0, 0, 0, $monthNumber));
datedoesn't respect the locale, strftimedoes.
date不尊重语言环境,strftime确实如此。
回答by Trent Renshaw
If you just want an array of month names from the beginning of the year to the end e.g. to populate a drop-down select, I would just use the following;
如果您只想要从年初到年底的月份名称数组,例如填充下拉选择,我将只使用以下内容;
for ($i = 0; $i < 12; ++$i) {
$months[$m] = $m = date("F", strtotime("January +$i months"));
}
回答by Nishad Up
There are many ways to print a month from the given number. Pick one suite for you.
有很多方法可以从给定的数字中打印一个月。为您挑选一间套房。
1. date() function along with parameter 'F'
1. date() 函数以及参数 'F'
Code example:
代码示例:
$month_num = 10;
echo date("F", mktime(0, 0, 0, $month_num, 10)); //output: October
2. By creating php date object using createFromFormat()
2. 通过使用 createFromFormat() 创建 php 日期对象
Code Example
代码示例
$dateObj = DateTime::createFromFormat('!m', $monthNum);
echo "month name: ".$dateObj->format('F'); // Output: October
3. strtotime() function
3. strtotime() 函数
echo date("F", strtotime('00-'.$monthNum.'-01')); // Output: October
4. mktime() function
4. mktime() 函数
echo date("F", mktime(null, null, null, $monthNum)); // Output: October
5. By using jdmonthname()
5. 通过使用 jdmonthname()
$jd=gregoriantojd($monthNum,10,2019);
echo jdmonthname($jd,0); // Output: Oct
回答by zzapper
adapt as required
根据需要适应
$m='08';
$months = array (1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec');
echo $months[(int)$m];
回答by Bryan Itur
If you have the month number, you can first create a date from it with a default date of 1st and default year of current year, then extract the month name from the date created:
如果您有月份编号,则可以首先从中创建一个默认日期为 1st 和当前年份的默认年份的日期,然后从创建的日期中提取月份名称:
echo date("F", strtotime(date("Y") ."-". $i ."-01"))
This code assume you have your month number stored in $i
此代码假设您将月份编号存储在 $i 中
Hope this helps someone
希望这有助于某人
回答by Matthew Park
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
I found this on https://css-tricks.com/snippets/php/change-month-number-to-month-name/and it worked perfectly.
我在https://css-tricks.com/snippets/php/change-month-number-to-month-name/上找到了这个,它运行得很好。

![php Laravel SQLSTATE[23000]:违反完整性约束:1062 重复条目](/res/img/loading.gif)