2009-12-23 55 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