项目开发中,获取2个时间点之间的年月是很常见的,特别在统计图表中。
$start_time = strtotime(date("Ymd 00:00:00",1519970368));
$end_time = time();
for($start_time; $start_time <= $end_time; $start_time = strtotime('+1 month', $start_time)){
$month_arr[] = date('Y-m',$start_time); // 取得递增月;
}
$month_arr 就是获取到的年月的数组。
Array
(
[0] => 2018-03
[1] => 2018-04
[2] => 2018-05
[3] => 2018-06
[4] => 2018-07
[5] => 2018-08
[6] => 2018-09
[7] => 2018-10
[8] => 2018-11
[9] => 2018-12
[10] => 2019-01
[11] => 2019-02
[12] => 2019-03
[13] => 2019-04
[14] => 2019-05
[15] => 2019-06
[16] => 2019-07