2009-12-23 56 views
2

提供下面的代碼,輸出是(見下文)。我的問題是爲什麼在2009/11/01之後,在2009/11/30和2009/12/30之前,而不是2009/12/01。從2009/06/01〜2009/11/01沒有問題。本月php的循環日期

輸出
2009/06/01
2009/07/01
2009/08/01
2009/09/01
2009/10/01
2009/11/01
2009 /30分之11
2009/12/30

我的代碼

<?php 

$startdate = "2009/06/01"; 
$enddate = "2009/12/31"; 

$start = strtotime($startdate); 
$end = strtotime($enddate); 

$currentdate = $start; 
while($currentdate < $end) 
{ 
    $cur_date = date('Y/m/d',$currentdate); 
    $month = date('m', $currentdate); 
    $year = date('Y', $currentdate); 
    $monthLength = daysOfMonth($month, $year); 
    $currentdate += $monthLength; 

    echo $cur_date . "<br />"; 
} 


function daysOfMonth($month, $year) 
{ 
    return (86400 * date("t", strtotime($year."-".$month."-01"))); 
} 

?> 
+0

是阿爾瓦羅的答案是否足夠?您應該將其標記爲「已接受」或對仍存在問題進行評論。 – philfreo 2009-12-26 04:37:12

回答

1
<?php 

$startdate = "2009/06/01"; 
$enddate = "2009/12/31"; 

$start = strtotime($startdate); 
$end = strtotime($enddate); 

$currentdate = $start; 
while($currentdate < $end) 
{ 
     $cur_date = date('Y/m/d', $currentdate); 

     $currentdate = strtotime('+1 month', $currentdate); 

     echo $cur_date . "<br />"; 
} 

?> 
+0

嗨阿爾瓦羅, 抱歉,由於聖誕假期,我遲到了回覆,我無法工作和閱讀你的答案。順便說一句,你的代碼完美的工作,如果開始日期不是從第01天開始,讓我們說$ startdate =「2009/06/06」? 非常感謝你 – tirso 2010-01-02 13:39:44

+0

這是一個新問題嗎?一旦你有一個Unix時間戳,你可以有很多功能來操作它。您可以使用date()提取片段,並使用mktime()生成新的時間戳。 – 2010-01-03 12:05:25

+0

嗨alvaro 謝謝。 Tirso – tirso 2010-01-04 01:11:27