2017-04-27 83 views
1

使用mktime获得月份不能在PHP 7.0中工作。PHP 7.0 mktime不能正常工作

$month_options=""; 
    for($i = 1; $i <= 12; $i++) { 
     $month_num = str_pad($i, 2, 0, STR_PAD_LEFT); 
     $month_name = date('F', mktime(0, 0, 0, $i + 1, 0, 0, 0)); 
     $selected=""; 
     $month_options.$month_name."<br/>"; 
    } 
    echo $month_options; 

结果在PHP 5.5

January 
February 
March 
April 
May 
June 
July 
August 
September 
October 
November 
December 

结果在7.0

January 
January 
January 
January 
January 
January 
January 
January 
January 
January 
January 

请帮助我如何reslove这个问题?..谢谢提前

+0

什么用的$ month_num?你为什么在mktime赚$ i + 1? – bfahmi

+0

它没有在使用,我忘记评论该线..吸收 – sridhard

+0

根据文档http://php.net/manual/en/function.mktime.php *“is_dst参数已被删除。”* –

回答

2

据克利里写here是最后一个参数的mktimeis_dst已经在PHP 7被删除,你必须给6个参数,而不是7

Try this code snippet here 7.0.8

<?php 

ini_set('display_errors', 1); 
$month_options = ""; 
for ($i = 1; $i <= 12; $i++) 
{ 
    $month_num = str_pad($i, 2, 0, STR_PAD_LEFT); 
    $month_name = date('F', mktime(0, 0, 0, $i + 1, 0, 0)); 
    $selected = ""; 
    $month_options .= $month_name . "<br/>"; 
} 
echo $month_options; 
+0

感谢其工作正常.. – sridhard

+0

@sridhard欢迎.... :) –

1

注意PHP7 = is _dst参数已被删除。

$month_options=""; 
for($i = 1; $i <= 12; $i++) { 
    /* $month_num = str_pad($i, 2, 0, STR_PAD_LEFT); -- there is no use for this line */ 
    $month_name = date('F', mktime(0, 0, 0, $i + 1, 0, 0)); // is_dst parameter has been removed. 
    /* $selected=""; -- there is no use for this line */ 
    /* $month_options.$month_name."<br/>"; -- you are not correctly set this paramter */ 
    $month_options .= $month_name."<br/>"; // so if you do like this, it will be correct 
} 
echo $month_options; 
1

为什么不使用DateTime对象呢?他们更容易操作,并且更容易操作。 DateTime可从PHP5.2及更高版本中获得。

这个片段

$date = new DateTime("january"); 
for ($i = 1; $i <= 12; $i++) { 
    echo $date->format("F")."\n"; 
    $date->modify("+1 months"); 
} 

将输出

January 
February 
March 
April 
May 
June 
July 
August 
September 
October 
November 
December 

Live demo