php循环按月的日期

时间:2009-12-23 08:01:18

标签: php

提供下述代码,输出结果如下(见下文)。我的问题是为什么在2009/11/01之后它会跟随2009/11/30和2009/12/30而不是2009/12/01。从2009/06/01~2009 / 11/01,没有问题。

输出
2009/06/01
2009/07/01
2009/08/01
2009/09/01
2009/10/01
2009/11/01
2009/11/30
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")));
} 

?>

1 个答案:

答案 0 :(得分: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 />";
}

?>