format($format) === $date; } /** * 获取两个日期之间的所有月份(格式:202508) * * @param string $startDate 开始日期(格式:Y-m-d) * @param string $endDate 结束日期(格式:Y-m-d) * @return array 月份数组,格式为 ['202508', '202509', ...] */ function getMonthsBetweenDates($startDate, $endDate) { $months = []; // 转换为 DateTime 对象 $start = new DateTime($startDate); $end = new DateTime($endDate); // 确保开始日期不大于结束日期 if ($start > $end) { return $months; } // 设置开始日期为当月的第一天 $start->modify('first day of this month'); // 循环直到超过结束日期 while ($start <= $end) { // 添加当前月份到数组(格式:202508) $months[] = $start->format('Ym'); // 增加一个月 $start->add(new DateInterval('P1M')); } return $months; }