简体   繁体   English

找出日期是否在两个日期之间,忽略年份

[英]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). 基本上,示例“当前日期”以红色突出显示,并且介于最近的日期(3月15日和11月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. 然而,当我使用Unix时间戳并人为地在日期中添加一年时,脚本认为11月22日还没有发生,因此它表明2月1日是在3月15日和11月22日之前,而不是在它们之间。 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. 在这种情况下,任何小于上限(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);
?>

Displays: boolean true 显示: 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". 如果您要检查的日期是“2月29日”且当前年份不是闰年,则DateTime会将其解释为“3月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. 要解决此问题,您可以将日期转换为MMDD形式的4位数字。

$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 ; 如果您的日期格式不正确,可以使用date('md', $timestamp) ,或者只需花费$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. 我正在使用词典排序,这是php比较字符串的方式。 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. 如果您还没有将日期作为这样格式化的字符串,则可以使用date('m-d', $timestamp)函数来实现。

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 工作代码在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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