PHP日期范围没有年份。 如何处理12月/ 1月环回[关闭]

I have user defined date ranges. A date range might be from November 8 - March 26 for example. I need to determine if a given date (without the year) is between one of these ranges. I don't want to include the year because the user defined date ranges are recursive for each year.

My problems is that I don't know how to handle the loop-back between December 31 and Jan 1. I'm guessing that a PHP DatePeriod object would be the way to go but how can I exclude the year from it? If I have a date range from December 10 to February 15 how can I determine if Jan 11 is in that range without specifying the year?

If you are really not dealing with years, then making one up (as per binnyb's answer) is the way to go. The trick is adding the logic to test if the start date is after the end date - indicating that they are from different years.

An example solution (based on the answer to a similar question with years):

// For convenience, build MM-DD strings
$date1 = "11-8";
$date2 = "03-26";
$test = "01-15";

if (check_in_range($date1, $date2, $test)) echo "in range!";

function check_in_range($start_date, $end_date, $test_date)
{
    $year = '2000';

    $start_ts = strtotime($year.'-'.$start_date);
    $end_ts = strtotime($year.'-'.$end_date);
    $test_ts = strtotime($year.'-'.$test_date);

    if ($start_ts > $end_ts) {
      $year -= 1;
      $start_ts = strtotime($year.'-'.$start_date);
    }

    return (($test_ts >= $start_ts) && ($test_ts <= $end_ts));
}

Since the year doesn't matter, use one year and always that year when comparing dates. Doing this allows you to create a valid DateTime object to use when checking if the test date is in range:

$constYear = '2000';
$dateStart = new DateTime($constYear . '-12-10');//December 10
$dateEnd = new DateTime($constYear . '-02-15');//February 15
$dateTest = new DateTime($constYear . '-01-11');//January 11
$isDateTestInRange = false;

if($dateTest > $dateStart && $dateTest < $dateEnd) {
  $isDateTestInRange = true;
}

Just be sure to not consider the year when using these variables ($dateStart, $dateEnd, $dateText) since it will include this constant $constYear(which is only used to create a valid date).

After much searching I found a thread with the solution... For anyone interested this is it...

Find out if date is between two dates, ignoring year