Displaying the list of months using mktime for the year 2012

戏子无情 提交于 2020-01-01 01:14:26

问题


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:

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!!!


回答1:


Try this:

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



回答2:


Months are same for every year

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



回答3:


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()

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



回答4:


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

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



回答5:


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.

list of setlocale codes



来源:https://stackoverflow.com/questions/10829424/displaying-the-list-of-months-using-mktime-for-the-year-2012

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!