php 如何从PHP中的数字获取月份名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6971135/
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
How to get the month name from a number in PHP?
提问by hd.
I have a variable containing a month number. How can I get the name of the month from this value?
我有一个包含月份数的变量。如何从此值中获取月份的名称?
I know I could define an array for $month_num => $month_name
, but I want to know if there is a time function in PHP that can do this, without the need for an array?
我知道我可以为 定义一个数组$month_num => $month_name
,但我想知道 PHP 中是否有一个时间函数可以做到这一点,而无需数组?
回答by Dreaded semicolon
date("F",mktime(0,0,0,$monthnumber,1,2011));
回答by Peter Ajtai
You can get just the textual month of a Unix time stamp with the F
date()
format character, and you can turn almost any format of date into a Unix time stamp with strtotime(), so pick any year and a day 1 to 28 (so it's present in all 12 months) and do:
您可以获得带有F
date()
格式字符的 Unix 时间戳的文本月份,并且您可以使用strtotime()将几乎任何格式的日期转换为 Unix 时间戳,因此选择任何一年和一天 1 到 28(因此它存在)在所有 12 个月内)并执行以下操作:
$written_month = date("F", strtotime("2001-$number_month-1"));
// Example - Note: The year and day are immaterial:
// 'April' == date("F", strtotime("2001-4-1"));
The nice thing about using strtotime()
is that it is very flexible. So let's say you want an array of textual month names that starts one month from whenever the script is run;
使用的好处strtotime()
是它非常灵活。因此,假设您想要一个文本月份名称数组,该数组从脚本运行时的一个月开始;
<?php
for ($number = 1; $number < 13; ++$number) {
// strtotime() understands the format "+x months"
$array[] = date("F", strtotime("+$number months"));
}
?>
回答by Danny Beckett
A slightly shorter version of the accepted answer is:
接受答案的略短版本是:
date('F', strtotime("2000-$monthnumber-01"));
F
stands for "month name", per the table ondate
.The
2000
is just a filler for the year, and the01
a filler for the day; since we're not concerned about anything other than the month name.
F
根据 上的表格,代表“月份名称”date
。这
2000
只是一年01
的填充物,以及当天的填充物;因为我们不关心月份名称以外的任何事情。
Here's a demoon ideone.
这是ideone上的演示。
回答by jmarceli
You could also use:
您还可以使用:
jdmonthname(gregoriantojd($monthnumber, 1, 1), CAL_MONTH_GREGORIAN_LONG)
It's just another way. I don't know anything about the efficiency of this compared to @Dreaded semicolon's answer.
这只是另一种方式。与@Dreaded分号的答案相比,我对它的效率一无所知。
Here's a demoon ideone.
这是ideone上的演示。
For reference:
以供参考:
jdmonthame
returns the Julian calendar month name.gregoriantojd
converts a Gregorian (currently used) calendar date to Julian (the1, 1
part stands for day and year).
jdmonthame
返回儒略历月份名称。gregoriantojd
将公历(当前使用的)日历日期转换为儒略(该1, 1
部分代表日和年)。
回答by Dayz
use the function mktime which takes the date elements as parameters.
使用函数 mktime 将日期元素作为参数。
<?php
$month_number= 3;
$month_name = date("F", mktime(0, 0, 0, $month_number, 10));
echo $month_name;
?>
Output: March
产出: 三月