user1610717
user1610717

Reputation: 325

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.

Determine if date falls between two dates

Upvotes: 8

Views: 7179

Answers (5)

Antony
Antony

Reputation: 4364

Here is my contribution based on the idea suggested by Roy M about using the Day of the Year. My query is to check if a date occurs during a Winter period spanning a year change.

private function isWinter(int $dateStr):bool {
    $input = date('z',$dateStr);

    // 2001 was NOT a leap year
    // Is $input between 27/10 and 30/3 (a winter period)
    $startDOY = date('z',strtotime('2001-10-27'));      

    $endDOY = date('z',strtotime('2001-3-30'));

    if (date('L',$dateStr)) {
        $startDOY++;
        $endDOY++;
    }

return $input <= $endDOY || $input >= $startDOY;
}

Upvotes: 0

Herbert
Herbert

Reputation: 5778

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;
}

Upvotes: 6

jcsanyi
jcsanyi

Reputation: 8174

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 

Upvotes: 1

goat
goat

Reputation: 31854

//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.

Upvotes: -1

Ayyappan Sekar
Ayyappan Sekar

Reputation: 11475

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

Upvotes: -1

Related Questions