如何获取当年和上一年的所有月份的开始和结束日期?

时间:2018-01-28 05:12:59

标签: php

我需要帮助来创建一个使用php via循环的数组,它将存储当前和上一年所有月份的开始和结束的秒数。该数组看起来有点像这样。

   $dates = array(
    'previous dates' => array( 
                        'November, 2017' => array( 
                                            'start' => 1512082800, 
                                            'end' => 1514674800),
                        'December, 2017' => array( 
                                            'start' => 1512082800, 
                                            'end' => 1514674800)
                        ),

    'current dates' => array( 
                        'January, 2018' => array( 
                                            'start' => 1512082800, 
                                            'end' => 1514674800),
                        'February, 2018' => array( 
                                            'start' => 1512082800, 
                                            'end' => 1514674800)
                            )
    )

1 个答案:

答案 0 :(得分:1)

$dates = array();
for ($year=2017; $year<=2018; $year++) {
    $yearOfDates = array();
    for ($month=1; $month<=12; $month++) {
        $begin = strtotime($year."-".$month."-01 12:00:00AM");
        $end = strtotime($year."-".($month+1)."-01 12:00:00AM")-1;
        $key = date("F, Y", $begin); // January, 2017
        $yearOfDates[$key] = array(
            'start' => $begin,
            'end' => $end
        );
    }
    if ($year==2017) $dates['previous year'] = $yearOfDates;
    else $dates['current year'] = $yearOfDates; //assume 2018
}