Find out if date is between two dates, ignoring year

前端 未结 4 1236
孤城傲影
孤城傲影 2021-01-18 12:20

I need to determine if a date (month and day) is between two other month/days.

I\'ve attached an image to this post that describes what I\'m trying to do. Basically

4条回答
  •  生来不讨喜
    2021-01-18 13:14

    The problem in your example is that (in the same year) the upper bounding date is before the lower bound. In that case, Any date less than the upper bound (Jan 1 - Mar 14) or greater than the lower bound (Nov 23 - Dec 31) falls between the two.

     $lowerBound;
        }
        var_dump($between);
    ?>
    

    Displays: boolean true

    Edit

    If the date you want to check is "Feb 29" and the current year is not a leap year, then DateTime interprets it as "Mar 1".

    To check if a date falls between two dates, inclusively, use:

    if ($lowerBound < $upperBound) {
        $between = $lowerBound <= $checkDate && $checkDate <= $upperBound;
    } else {
        $between = $checkDate <= $upperBound || $checkDate >= $lowerBound;
    }
    

提交回复
热议问题