DateUtil.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /**
  38. * 获取某个时间段内所有月份 (不包含开头月份,包含结尾月份)
  39. * @param minDate
  40. * @param maxDate
  41. * @return
  42. * @throws ParseException
  43. */
  44. public static function getMonthBetweenDatesNotBegin($minDate, $maxDate) {
  45. $sTime = strtotime(date('Y-m-01', strtotime($minDate)));
  46. $eTime = strtotime(date('Y-m-01', strtotime($maxDate)));
  47. $months = [];
  48. $sTime = strtotime("next month", $sTime); //第一个月跳过
  49. while ($sTime <= $eTime) {
  50. $months[] = date('Y-m', $sTime);
  51. $sTime = strtotime('next month', $sTime);
  52. }
  53. return $months;
  54. }
  55. public static function getEveryMonthDayBetween($startTime, $endTime, $monthList) {
  56. $res = [];
  57. $sdf = new DateTimeFormatter('yyyy-MM-dd');
  58. $min = new DateTime($startTime);
  59. $min->setTime(0, 0, 0);
  60. $max = new DateTime($endTime);
  61. $max->setTime(23, 59, 59);
  62. $curr = clone $min;
  63. while ($curr < $max) {
  64. $month = $curr->format('Y-m');
  65. if (in_array($month, $monthList)) {
  66. if (!isset($res[$month])) {
  67. $res[$month] = 0;
  68. }
  69. $res[$month] ++;
  70. }
  71. $curr->modify('+1 day');
  72. }
  73. return $res;
  74. }
  75. }