<? /*
* 作者:心灯
* 功能:实现从指定时间向下推N个月的函数
* month($ymd,$len) $ymd时间,$len推出几个月
* $ymd='2005-01-01';
*/
function month($ymd='',$len='12'){$month = array();
$lang = $lang."<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">";
if(
$ymd){//判断时间格式是否正确if(!ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})",$ymd)){
echo
"<script>alert('函数参数中的日期格式不对');history.back()</script>";exit;
}
}
if(
$len){if(!
ereg("[0-9]+",$len)){echo
$lang."<script>alert('函数中的时间长度中含有非法字段');history.back()</script>";exit;
}
}
for(
$i=0;$i<$len;$i++){if(
$i==0){$mktime = $ymd?strtotime($ymd):time();
}
$month[] = date("Y-m",$mktime);//可以根据需要来设计置你要的格式
$day = date("t",$mktime);
if(
$i==0)$reday = date("d",$mktime);
else
$reday = $day;
$t1 = $mktime;
$mktime=date("Y-m-d",mktime(date("H",$t1),date("i",$t1),date("s",$t1),date("m",$t1),date("d",$t1)-$reday,date("Y",$t1)));
$mktime =strtotime($mktime);
}
return
$month;}
$month = month();foreach(
$month as $key => $v){echo
$v."<br>";}
?>效果如下:
2005-01
2004-12
2004-11
2004-10
2004-09
2004-08
2004-07
2004-06
2004-05
2004-04
2004-03
2004-02