简体   繁体   中英

Find out if date is between two dates, ignoring year

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 the example "current day" is highlighted in red, and it's between those nearest dates (Mar 15 and Nov 22). However, when I use Unix timestamps and artificially add a year to the dates, the script thinks that Nov 22 hasn't occurred yet, and therefore it suggests that Feb 1 is before Mar 15 AND Nov 22, instead of in between them. So, I need to compare dates without using a year.

Hopefully this makes sense. I'd just like to compare dates using months and days, but ignoring the year, and using a framework like the wheel I show in the image.

确定日期是否介于两个日期之间

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.

<?php
    $upperBound = new DateTime("Mar 15");
    $lowerBound = new DateTime("Nov 22");
    $checkDate = new DateTime("Feb 1");

    if ($lowerBound < $upperBound) {
        $between = $lowerBound < $checkDate && $checkDate < $upperBound;
    } else {
        $between = $checkDate < $upperBound || $checkDate > $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;
}

To solve this, you can convert your dates to 4-digit numbers of the form MMDD.

$start = '1122';
$end = '0315';
$date = '0201';

If you don't yet have your dates formatted like this, you can use date('md', $timestamp) , or just calculate the number by taking $month * 100 + $day ;

Then your test logic will change depending on whether you're crossing a year boundary or not. Putting it all together, it might look like this:

function isDateBetween($testM, $testD, $startM, $startD, $endM, $endD) {
    $start = $startM*100 + $startD;
    $end = $endM*100 + $endD;
    $date = $testM*100 + $testD;

    if ($start > $end) {
        // crossing a year boundary
        return ($date > $start) || ($date < $end);
    } else {
        // not crossing a year
        return ($date > $start) && ($date < $end);
    }
}

Here's some examples of calling this function:

// your example given above
isDateBetween(2, 1, 11, 22, 3, 15); // true

// ends the day after instead
isDateBetween(3, 16, 11, 22, 3, 15); // false

// reverse the dates in your example
isDateBetween(2, 1, 3, 15, 11, 22); // false

// ends the day after, with reversed dates
isDateBetween(3, 16, 3, 15, 11, 22); // true 
//must be 2 digit month, then 2 digit day, like mm-dd
$low  = '03-15';
$high = '11-22';
$date = '02-01';

$isBetween = $date >= $low && $date <= $high;
var_dump($isBetween);

I'm making use of lexicographic ordering, which is how php compares strings. The key point is to put the largest units on the left(months), and make sure to left-pad each numeric segment to the same length, using zeros to pad.

If you don't yet have your dates as strings formatted like this, you can use date('m-d', $timestamp) function to achieve that.

Try like this.

<?php
$check_date= strtotime("31-Aug-1990");
$start_date= strtotime("10-Aug-2013");
$end_date= strtotime("30-Aug-1990");

if (date("M-d", $check_date)>date("M-d", $start_date) && date("M-d", $check_date)<date("M-d", $end_date))
    echo "in the range";
else
    echo "not in the range";

Working code is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM