dongshan1036
2018-01-28 05:12
浏览 604
已采纳

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

I need help to create an array using php via loop that will store the number of seconds of start and end of all the months of current and previous year. The array will look somewhat like this.

   $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条回答 默认 最新

  • doutouman6245 2018-01-28 05:42
    最佳回答
    $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
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题