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