如何获得本月起的未来12个月?
我现在用的是:
- PHP code
for($i=1;$i<=12;$i++){
$nextdate = date("Y-m",strtotime("+ $i month"));
echo ''.$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
------解决方案--------------------
因为 你的那个月份都是按30天算的 有的月份是28天的 所以等到了二月就不对了
------解决方案--------------------
- PHP code
// 参数表示好多个月,你也可以修改为指定日期开始
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() );