确定如果两个时间段的任何一点重叠 [英] Determining if two time ranges overlap at any point

查看:201
本文介绍了确定如果两个时间段的任何一点重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  确定是否两个日期范围重叠

我试图找出如果两个时间段在PHP重叠。我已经提到确定是否两个日期范围重叠了解我最初的尝试然而,它不匹配的所有情况。如果一个时间段嵌套在另一个时间段的开始和结束时间之间,它没有被匹配。如果它的开始或移位结束时,重叠或如果移位是准确匹配,它按预期

I am trying to work out if two time ranges in PHP overlap. I've been referring to Determine Whether Two Date Ranges Overlap for my initial try, however, it's not matching all cases. If a time range is nested in between the start and end times of another time range, it's not being matched. If it overlaps the beginning or the end of the shift, or if the shifts are exact matches, it works as expected.

检查出什么我谈论这个图片:

Check out this image of what I'm talking about:

基本上,我想,如果他们在任何地方任何重叠红移隐瞒任何橙色的变化。下面是code我试图用它来实现这一目标的相关部分。

Basically, I am trying to hide any orange shifts if they overlap any red shifts anywhere. Here's the relevant portion of code I'm trying to use to make this happen.

if(($red['start'] <= $orange['end']) && ($red['end'] >= $orange['start'])) {
    //Conflict handling
}

的变量的值是UNIX的时间戳。通过数字逻辑上的工作,我明白了为什么上面的语句失败。明明有方法,我可以做更多的逻辑,以确定是否一个班次落在其他移(这是我需要做的),但我希望一个更普遍的陷阱。

The values of the variables are UNIX timestamps. Working through the numbers logically, I understand why the statement above fails. There are obviously ways I could do more logic to determine if the one shift falls in the other shift (which is what I may need to do), but I was hoping for a more universal catch.

编辑:添加每个块的开始和结束时间的值。我同意我有什么的应该的工作。事实上,这并非是我的问题所在。我可能忽视的东西愚蠢的。

Adding the values of each block's start and end time. I agree what I have should work. The fact that it isn't is where my issue lies. I'm probably overlooking something dumb.

orange-start = 1352899800
orange-end = 1352907000

red-start = 1352923200
red-end = 1352926200

因此​​我的逻辑将陈述:

Therefore my logic would state:

if((1352923200 <= 1352907000) && (1352926200 >= 1352899800))

所以以下是,在第一比较失败。

So following that, the first comparison fails.

编辑2:它看起来像我的逻辑是合理的(我认为是这样),我的问题是,一些相关的UNIX时间戳不匹配的实际时间显示。我感谢那些谁的工作,虽然这与我和帮助我发现,作为这个问题。我希望我能同时接受安德烈的和贾森的回答。

EDIT 2: It looks like my logic is sound (which I thought was the case), and my issue is something related to the UNIX timestamp not matching the actual time being displayed. I thank those who worked though this with me and help me discover that as being the issue. I wish I could accept both Andrey's and Jason's answers.

推荐答案

的逻辑是正确的。 (:50pm 8-8)和 $橙色(1:30-3您为 $红色的时间戳: 30PM)不重叠

The logic is correct. The timestamps you provided for $red (8-8:50pm) and $orange (1:30-3:30pm) do not overlap.

由于正确的值(即反映您的截图),重叠的确发现:

Given correct values (that reflect your screenshot), the overlap is indeed found:

function show_date($value, $key) {
    echo $key, ': ', date('r', $value), PHP_EOL;
}

$red = array('start' => strtotime('today, 2pm'), 'end' => strtotime('today, 2:45pm'));
$orange = array('start' => strtotime('today, 1:30pm'), 'end' => strtotime('today, 4pm'));

array_walk($red, 'show_date');
array_walk($orange, 'show_date');

if (($red['start'] <= $orange['end']) && ($red['end'] >= $orange['start'])) {
    echo 'Conflict handling';
}

我的猜测是,你有一个时区转换的问题。

My guess would be you have a timezone conversion issue.

这篇关于确定如果两个时间段的任何一点重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆