如何获取当年和上一年的所有月份的开始和结束日期?
问题描述:
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)
)
)
答
$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
}