如何获得本月起的未来12个月?我现在用的是:for($i=1;$i<=12;$i++){
$nextdate = date("Y-m",strtotime("+ $i month"));
echo '<p>'.$nextdate;
}
看起来似乎没问题,可打印后却发现3月份是重复的,不知是何原因。2009-11
2009-12
2010-01
2010-03
2010-03
2010-04
2010-05
2010-06
2010-07
2010-08
2010-09
2010-10

解决方案 »

  1.   


    for($i=1;$i<=12;$i++){
        $nextdate = date("Y-m-d",strtotime("+ $i month"));
        echo '<p>'.$nextdate;
    }仅仅通过秒数来计算,显然不行了。
      

  2.   

    function c($num = 12){
        $temp = array();
        $nowtime = strtotime( date('Y-m', time()) . '-1' );
        while( $num>0 ){
            $num--;
            $nowtime = strtotime( '+1 month', $nowtime );
            $temp[]  = date('Y-m', $nowtime);
        }
        unset( $num, $nowtime );
        return $temp;
    }print_r( c() );
      

  3.   


    // 参数表示好多个月,你也可以修改为指定日期开始
    function c($num = 12){
        $temp = array();
        $nowtime = strtotime( date('Y-m', time()) . '-1' );
        while( $num>0 ){
            $num--;
            $nowtime = strtotime( '+1 month', $nowtime );
            $temp[]  = date('Y-m', $nowtime);
        }
        unset( $num, $nowtime );
        return $temp;
    }print_r( c() );
      

  4.   

    谢谢各位,我把月份改成天就可以了$nextdate = date("Y-m",strtotime("+ $mday day"));