廿八、获取两个年月中间的所有月初,月末的时间戳

2019-03-20  本文已影响0人  yuzhan550

1. 比如:2016-08 -> 2019.04

function month_io($y1,$m1,$y2,$m2){  // (2016,08,2019,04)
    //1. 补全  start年的月数
    if( $m1 !=12 ){
        for( $i=$m1;$i<=12;$i++ ){
            $res[]['y_m'] = $y1 . '-' . $i;
        }
    }
    unset($i);
    //2. 始末年份的差值
    if( ($y1+1)!=$y2  ){
        for( $i=$y1+1;$i<$y2;$i++ ){
            for($j=1;$j<=12;$j++){
                $res[]['y_m'] = $i . '-' . $j;
            }
        }
    }
    unset($i);
    unset($j);
    //3. 今年的月份
    for($i=1;$i<=$m2;$i++){
        $res[]['y_m'] = $y2 . '-' . $i;
    }
    //4. 遍历这个array,存入戳
    foreach ($res as $k=>$v){
        $res[$k]['month_start_io'] = strtotime($v['y_m']);
        $mm = date('m',strtotime($v['y_m']));
        $dd = date('t',strtotime($v['y_m']));
        $yy = date('Y',strtotime($v['y_m']));
        $res[$k]['month_end_io'] = mktime(23, 59, 59,$mm,$dd,$yy);
    }
    return $res;
}

2. 结果:

Array
(
[0] => Array
    (
        [y_m] => 2017-8
        [month_start_io] => 1501516800
        [month_end_io] => 1504195199
    )

[1] => Array
    (
        [y_m] => 2017-9
        [month_start_io] => 1504195200
        [month_end_io] => 1506787199
    )

[2] => Array
    (
        [y_m] => 2017-10
        [month_start_io] => 1506787200
        [month_end_io] => 1509465599
    )

[3] => Array
    (
        [y_m] => 2017-11
        [month_start_io] => 1509465600
        [month_end_io] => 1512057599
    )

[4] => Array
    (
        [y_m] => 2017-12
        [month_start_io] => 1512057600
        [month_end_io] => 1514735999
    )

[5] => Array
    (
        [y_m] => 2018-1
        [month_start_io] => 1514736000
        [month_end_io] => 1517414399
    )

[6] => Array
    (
        [y_m] => 2018-2
        [month_start_io] => 1517414400
        [month_end_io] => 1519833599
    )

[7] => Array
    (
        [y_m] => 2018-3
        [month_start_io] => 1519833600
        [month_end_io] => 1522511999
    )

[8] => Array
    (
        [y_m] => 2018-4
        [month_start_io] => 1522512000
        [month_end_io] => 1525103999
    )

[9] => Array
    (
        [y_m] => 2018-5
        [month_start_io] => 1525104000
        [month_end_io] => 1527782399
    )

[10] => Array
    (
        [y_m] => 2018-6
        [month_start_io] => 1527782400
        [month_end_io] => 1530374399
    )

[11] => Array
    (
        [y_m] => 2018-7
        [month_start_io] => 1530374400
        [month_end_io] => 1533052799
    )

[12] => Array
    (
        [y_m] => 2018-8
        [month_start_io] => 1533052800
        [month_end_io] => 1535731199
    )

[13] => Array
    (
        [y_m] => 2018-9
        [month_start_io] => 1535731200
        [month_end_io] => 1538323199
    )

[14] => Array
    (
        [y_m] => 2018-10
        [month_start_io] => 1538323200
        [month_end_io] => 1541001599
    )

[15] => Array
    (
        [y_m] => 2018-11
        [month_start_io] => 1541001600
        [month_end_io] => 1543593599
    )

[16] => Array
    (
        [y_m] => 2018-12
        [month_start_io] => 1543593600
        [month_end_io] => 1546271999
    )

[17] => Array
    (
        [y_m] => 2019-1
        [month_start_io] => 1546272000
        [month_end_io] => 1548950399
    )

[18] => Array
    (
        [y_m] => 2019-2
        [month_start_io] => 1548950400
        [month_end_io] => 1551369599
    )

[19] => Array
    (
        [y_m] => 2019-3
        [month_start_io] => 1551369600
        [month_end_io] => 1554047999
    )

[20] => Array
    (
        [y_m] => 2019-4
        [month_start_io] => 1554048000
        [month_end_io] => 1556639999
    )

[21] => Array
    (
        [y_m] => 2019-5
        [month_start_io] => 1556640000
        [month_end_io] => 1559318399
    )

)
上一篇下一篇

猜你喜欢

热点阅读