php 使用 mktime 显示 2012 年的月份列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10829424/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:08:36  来源:igfitidea点击:

Displaying the list of months using mktime for the year 2012

phpdatemktime

提问by Sboniso Marcus Nzimande

Am am current facing a problem that need a solution ASAP.

我目前面临一个需要尽快解决的问题。

I am trying to list all months of the current year(2012) by using the following code:

我正在尝试使用以下代码列出当前年份(2012)的所有月份:

for ($m=1; $m<=12; $m++) {
     $month = date('F', mktime(0,0,0,$m));
     echo $month. '<br>';
     }

But am getting the following unexpected output:

但我得到以下意外输出:

January March March May May July July August October October December December

一月三月三月五月七月七月八月十月十月十二月

What am I doing wrong please help!!!

我做错了什么请帮忙!!!

回答by iWizard

Try this:

尝试这个:

for ($m=1; $m<=12; $m++) {
     $month = date('F', mktime(0,0,0,$m, 1, date('Y')));
     echo $month. '<br>';
     }

回答by Raab

Months are same for every year

每年的月份都一样

$array = array("January", "February",.....);
for ($m=0; $m<12; $m++) {
     echo $array[$m]. '<br>';
     }

回答by sephoy08

I guess you should loop it in this manner.

我想你应该以这种方式循环它。

for($i = 1 ; $i <= 12; $i++)
{
 echo date("F",strtotime(date("Y")."-".$i."-01"));
 echo "<br/>";
}

Or in your case, you want to use mktime()

或者在您的情况下,您想使用 mktime()

for($i = 1 ; $i <= 12; $i++)
{
 echo date("F",mktime(0,0,0,$i,1,date("Y")));
 echo "<br/>";
}

回答by Jan Turoň

Set dayin mktime()to 1, otherwise conversion is performed: 30.2.2012 = 1.3.2012

坐落daymktime()1,否则执行转换:30.2.2012 = 1.3.2012

$month = date('F', mktime(0,0,0,$m,1));

回答by omabra

Pay attention to the localization.

注意本地化。

You can also use this

你也可以用这个

setlocale(LC_TIME, 'it_IT');
for($m=1;$m<=12;$m++){
  echo strftime("%B", mktime(0, 0, 0, $m, 12));
}

Changing the parameter on the function setlocale() you can display the localized text.

更改函数 setlocale() 上的参数可以显示本地化文本。

list of setlocale codes

setlocale 代码列表