根据格式为YYYY-MM的两个日期, 求出这段日期中的月份数

最近在做安装量和下载量的统计的时候, 需要求出这样的数组.

function getDuringMonths($start, $end){
    //转为时间戳
    $start     = strtotime($start.'-01');
    $end     = strtotime($end.'-01');
    $i         = 0;
    $d         = array();
    while( $start <= $end ){
        //这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
        $d[$i] = trim(date('Y-m',$start),' ');
        $start += strtotime('+1 month',$start)-$start;
        $i++;
    }
    return $d;
}

例子:

getDuringMonths('2013-07', '2015-06');

返回这段时间内的所有月份, 格式为YYYY-MM

posted @ 2015-06-26 16:06  Zell~Dincht  阅读(513)  评论(0编辑  收藏  举报