2017-04-27 82 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