当前位置:Gxlcms > PHP教程 > PHP遍历月度可以做到吗

PHP遍历月度可以做到吗

时间:2021-07-01 10:21:17 帮助过:6人阅读

PHP遍历月份可以做到吗?
比如我已知
起始月份2014-08-12
截止月份2015-10-20

遍历这个时间段的月历
例如:
2014-08
2014-09
2014-10
2014-11
2014-12
2015-01
2015-02
。。。。。
2015-10




------解决思路----------------------

$startdate = '2014-08-12';
$enddate = '2015-10-20';

$s = strtotime($startdate);
$e = strtotime($enddate);

$num = (date('Y',$e)-date('Y',$s)-1)*12+(12-date('m',$s)+1)+date('m',$e);

$months = array();
for($i=0; $i<$num; $i++){
$d = mktime(0,0,0,date('m',$s)+$i,date('d',$s),date('Y',$s));
$months[] = date('Y-m',$d);
}

print_r($months);



Array
(
[0] => 2014-08
[1] => 2014-09
[2] => 2014-10
[3] => 2014-11
[4] => 2014-12
[5] => 2015-01
[6] => 2015-02
[7] => 2015-03
[8] => 2015-04
[9] => 2015-05
[10] => 2015-06
[11] => 2015-07
[12] => 2015-08
[13] => 2015-09
[14] => 2015-10
)

人气教程排行