6

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

4

4 回答 4

5

您的示例中的问题是(在同一年)上限日期在下限之前。在这种情况下,任何小于上限(1 月 1 日至 3 月 14 日)或大于下限(11 月 23 日至 12 月 31 日)的日期都介于两者之间。

<?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);
?>

显示: boolean true

编辑

如果您要检查的日期是“2 月 29 日”并且当前年份不是闰年,则 DateTime 将其解释为“3 月 1 日”。

要检查日期是否介于两个日期之间,包括在内,请使用:

if ($lowerBound < $upperBound) {
    $between = $lowerBound <= $checkDate && $checkDate <= $upperBound;
} else {
    $between = $checkDate <= $upperBound || $checkDate >= $lowerBound;
}
于 2013-07-09T06:17:18.293 回答
0

要解决此问题,您可以将日期转换为 MMDD 形式的 4 位数字。

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

如果您还没有像这样格式化您的日期,您可以使用date('md', $timestamp),或者只是通过计算来计算数字$month * 100 + $day;

然后,您的测试逻辑将根据您是否跨越年份而改变。把它们放在一起,它可能看起来像这样:

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

以下是调用此函数的一些示例:

// 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 
于 2013-07-09T06:00:37.290 回答
-1
//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);

我正在使用字典顺序,这是 php 比较字符串的方式。关键是将最大的单位放在左侧(月),并确保将每个数字段的左侧填充到相同的长度,使用零填充。

如果您的日期还没有像这样格式化的字符串,您可以使用date('m-d', $timestamp)函数来实现。

于 2013-07-09T05:41:13.497 回答
-1

像这样试试。

<?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";

工作代码在这里

于 2013-07-09T05:46:43.230 回答