Home >Backend Development >PHP Tutorial >PHP遍历月份可以做到吗?

PHP遍历月份可以做到吗?

WBOY
WBOYOriginal
2016-06-23 13:24:231637browse

比如我已知
起始月份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
)

太感谢了!楼上牛人!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn