DateUtil.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. class DateUtil {
  3. /**
  4. * 获取某个时间段内所有月份 (包含开头结尾月份)
  5. * @param minDate
  6. * @param maxDate
  7. * @return
  8. * @throws ParseException
  9. */
  10. public static function getMonthBetweenDates($minDate, $maxDate) {
  11. $sTime = strtotime(date('Y-m-01', strtotime($minDate)));
  12. $eTime = strtotime(date('Y-m-01', strtotime($maxDate)));
  13. $months = [];
  14. while ($sTime <= $eTime) {
  15. $months[] = date('Y-m', $sTime);
  16. $sTime = strtotime('next month', $sTime);
  17. }
  18. return $months;
  19. }
  20. /**
  21. * 获取某个时间段内所有月份 (包含开头不包含结尾月份)
  22. * @param minDate
  23. * @param maxDate
  24. * @return
  25. * @throws ParseException
  26. */
  27. public static function getMonthBetweenDatesNotEnd($minDate, $maxDate) {
  28. $sTime = strtotime(date('Y-m-01', strtotime($minDate)));
  29. $eTime = strtotime(date('Y-m-01', strtotime($maxDate)));
  30. $months = [];
  31. while ($sTime < $eTime) {
  32. $months[] = date('Y-m', $sTime);
  33. $sTime = strtotime('next month', $sTime);
  34. }
  35. return $months;
  36. }
  37. }