检查日期范围(开始日期和结束日期)是否重叠

2023-12-29

function checkDateOverlap($ranges) {
    $res = $ranges[0];
    $countRanges = count($ranges);

    for ($i = 0; $i < $countRanges; $i++) {

        $r1s = $res['start'];
        $r1e = $res['end'];

        $r2s = $ranges[$i]['start'];
        $r2e = $ranges[$i]['end'];

        if ($r1s >= $r2s && $r1s <= $r2e || $r1e >= $r2s && $r1e <= $r2e || $r2s >= $r1s && $r2s <= $r1e || $r2e >= $r1s && $r2e <= $r1e) {
            $res = array(
                'start' => $r1s > $r2s ? $r1s : $r2s,
                'end' => $r1e < $r2e ? $r1e : $r2e
            );
        } else 
            return false;
    }
    return $res;
}
// example of returned dates that overlap
$ranges = array(
    array('start' => '2014-01-01', 'end' => '2014-01-04'),
    array('start' => '2014-01-05', 'end' => '2014-01-10'),
    array('start' => '2014-01-04', 'end' => '2014-01-07')
);
//example of failure
$ranges2 = array(
        array('start' => '2014-01-01', 'end' => '2014-01-04'),
        array('start' => '2014-01-05', 'end' => '2014-01-10'),
        array('start' => '2014-01-11', 'end' => '2014-01-17')
    );

var_dump(checkDateOverlap($ranges));

以下是我试图检查日期范围交集的内容。在数组“ranges1”中,此示例具有重叠的日期。它应该返回日期。在数组 $ranges2 中,这应该作为没有相交的日期传递。

现在奇怪的是开始日期和结束日期可以完全相同,因此您可以仅输入一天的条目。我尝试了很多事情,但我很困惑。

我相信需要另一个 for 循环,但无论如何我都没有取得成功。

这是我的另一个尝试:

<?php

// 将你的范围传递给这个方法,如果有一个共同的交点,它会 // 返回或者返回 false

function checkDateOverlap($ranges){
    $res = $ranges[0];
    $countRanges = count($ranges);
    for ($i = 0; $i < count($countRanges); $i++) {
        for($j = $i+1; $j < count($countRanges); $j++) {
            $r1s = $res['start'];
            $r1e = $res['end'];

            $r2s = $ranges[$i]['start'];
            $r2e = $ranges[$i]['end'];

            if (($r1s >= $r2e && $r2s <= $r1e)) {

                $res[] = array(
                    'start' => $r1s > $r2s ? $r1s : $r2s,
                    'end' => $r1e < $r2e ? $r1e : $r2e
                );

            } else 
                return false;
        }
    }
    return $res;
}

// example
$ranges = array(
    array('start' => '2014-01-04', 'end' => '2014-01-05'),
    array('start' => '2014-01-06', 'end' => '2014-01-10'),
    array('start' => '2014-01-11', 'end' => '2014-01-13')
);

echo "<pre>";

var_dump(checkDateOverlap($ranges));
echo "</pre>";  

任何建议都非常感激。


$ranges = array(
        array('start' => new DateTime('2014-01-01'), 'end' => new DateTime('2014-01-05')),
        array('start' => new DateTime('2014-01-06'), 'end' => new DateTime('2014-01-06')),
        array('start' => new DateTime('2014-01-07'), 'end' => new DateTime('2014-01-07')),
    );

    function intersects($lhs, $rhs) {
        // Note that this function allows ranges that "touch", 
        // eg. one pair starts at the exact same time that the other ends.
        // Adding less "or equal to" will allow same start date 
        return !($lhs['start'] > $rhs['end'] || $lhs['end'] < $rhs['start']);
    }

    function checkDates($ranges) {
        // Comparison loop is of size n•log(n), not doing any redundant comparisons
        for($i = 0; $i < sizeof($ranges); $i++) {
            for($j = $i+1; $j < sizeof($ranges); $j++) {
                if(intersects($ranges[$i], $ranges[$j])) {
                    echo "Date {$i} intersects with date {$j}\n";
                }
            }
        }
    }

    checkDates($ranges);

我附上了我的工作代码示例,希望能帮助其他人在将来寻找相同的解决方案。这将打印相交的数组。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检查日期范围(开始日期和结束日期)是否重叠 的相关文章

随机推荐