12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?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 = [];
- $sdf = new DateTimeFormatter('yyyy-MM-dd');
- $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;
- }
- }
|