如何从PHP中的数字中获取月份名称?

时间:2022-08-25 10:56:50

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中是否有时间函数可以做到这一点,而不需要数组?

5 个解决方案

#1


22  

date("F",mktime(0,0,0,$monthnumber,1,2011));

#2


14  

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

Working example

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"));
}
?>

Working example

#3


5  

A slightly shorter version of the accepted answer is:

接受答案的略短版本是:

date('F', strtotime("2000-$monthnumber-01"));
  • F stands for "month name", per the table on date.

    根据日期表格,F代表“月份名称”。

  • The 2000 is just a filler for the year, and the 01 a filler for the day; since we're not concerned about anything other than the month name.

    2000年只是当年的填充物,01是当天的填充物;因为我们不关心月份名称以外的任何事情。

Here's a demo on ideone.

这是关于ideone的演示。

#4


2  

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 demo on ideone.

这是关于ideone的演示。

For reference:

  • jdmonthame returns the Julian calendar month name.

    jdmonthame返回Julian日历月名称。

  • gregoriantojd converts a Gregorian (currently used) calendar date to Julian (the 1, 1 part stands for day and year).

    gregoriantojd将Gregorian(当前使用的)日历日期转换为Julian(1,1个部分代表日期和年份)。

#5


0  

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

#1


22  

date("F",mktime(0,0,0,$monthnumber,1,2011));

#2


14  

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

Working example

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"));
}
?>

Working example

#3


5  

A slightly shorter version of the accepted answer is:

接受答案的略短版本是:

date('F', strtotime("2000-$monthnumber-01"));
  • F stands for "month name", per the table on date.

    根据日期表格,F代表“月份名称”。

  • The 2000 is just a filler for the year, and the 01 a filler for the day; since we're not concerned about anything other than the month name.

    2000年只是当年的填充物,01是当天的填充物;因为我们不关心月份名称以外的任何事情。

Here's a demo on ideone.

这是关于ideone的演示。

#4


2  

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 demo on ideone.

这是关于ideone的演示。

For reference:

  • jdmonthame returns the Julian calendar month name.

    jdmonthame返回Julian日历月名称。

  • gregoriantojd converts a Gregorian (currently used) calendar date to Julian (the 1, 1 part stands for day and year).

    gregoriantojd将Gregorian(当前使用的)日历日期转换为Julian(1,1个部分代表日期和年份)。

#5


0  

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