/** * 获取两个日期之间所有的月份 * @param string $date1 - 起始日期 默认1970-01-01 * @param string $date2 - 截止日期 默认1970-02-01 * @return array */ public function getMonthNum(string $date1 = '1970-01-01', string $date2 = '1970-02-01'):array { $time1 = strtotime($date1); // 自动为00:00:00 时分秒 $time2 = strtotime($date2); if ($time2 < $time1){ return []; } $month = array(); $split_date1 = explode('-',$date1); $month[] = $split_date1[0].'-'.$split_date1[1];//初始日期 while( ($time1 = strtotime('+1 month', $time1)) <= $time2){ $month[] = date('Y-m',$time1); // 取得递增月; } return $month; }