123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- class DateUtil {
- /**
- * 获取某个时间段内所有月份 (包含开头结尾月份)
- * @param minDate
- * @param maxDate
- * @return
- * @throws ParseException
- */
- public static function getMonthBetweenDates($minDate, $maxDate) {
- $sTime = strtotime(date('Y-m-01', strtotime($minDate)));
- $eTime = strtotime(date('Y-m-01', strtotime($maxDate)));
- $months = [];
- while ($sTime <= $eTime) {
- $months[] = date('Y-m', $sTime);
- $sTime = strtotime('next month', $sTime);
- }
- return $months;
- }
- /**
- * 获取某个时间段内所有月份 (包含开头月份,不包含结尾月份)
- * @param minDate
- * @param maxDate
- * @return
- * @throws ParseException
- */
- public static function getMonthBetweenDatesNotEnd($minDate, $maxDate) {
- $sTime = strtotime(date('Y-m-01', strtotime($minDate)));
- $eTime = strtotime(date('Y-m-01', strtotime($maxDate)));
- $months = [];
- while ($sTime < $eTime) {
- $months[] = date('Y-m', $sTime);
- $sTime = strtotime('next month', $sTime);
- }
- return $months;
- }
- /**
- * 获取某个时间段内所有月份 (不包含开头月份,包含结尾月份)
- * @param minDate
- * @param maxDate
- * @return
- * @throws ParseException
- */
- public static function getMonthBetweenDatesNotBegin($minDate, $maxDate) {
- $sTime = strtotime(date('Y-m-01', strtotime($minDate)));
- $eTime = strtotime(date('Y-m-01', strtotime($maxDate)));
- $months = [];
- $sTime = strtotime("next month", $sTime); //第一个月跳过
- while ($sTime <= $eTime) {
- $months[] = date('Y-m', $sTime);
- $sTime = strtotime('next month', $sTime);
- }
- return $months;
- }
- public static function getEveryMonthDayBetween($startTime, $endTime, $monthList) {
- $res = [];
- $min = new DateTime($startTime);
- $min->setTime(0, 0, 0);
- $max = new DateTime($endTime);
- $max->setTime(23, 59, 59);
- $curr = clone $min;
- while ($curr < $max) {
- $month = $curr->format('Y-m');
- if (in_array($month, $monthList)) {
- if (!isset($res[$month])) {
- $res[$month] = 0;
- }
- $res[$month] ++;
- }
- $curr->modify('+1 day');
- }
- return $res;
- }
- }
|