[英]Overlapping of two date/time ranges - PHP
I am trying to determine if two sets of date/time range overlap or not --- returning BOOLEAN value. 我正在尝试确定两组日期/时间范围是否重叠-返回BOOLEAN值。
Consider the following cases: 考虑以下情况:
NOTE : I turn each date/time string below (eg 24-JAN-17 07:00
) to timestamp using strtotime()
注意 :我使用strtotime()
下面的每个日期/时间字符串(例如24-JAN-17 07:00
)转为时间戳。
Expression used for each case is: $isOverlapping = (s1 < e2 && s2 < e1)
每种情况下使用的表达式为: $isOverlapping = (s1 < e2 && s2 < e1)
Case 1 : 情况1 :
Let (s1, e1) be defined as (24-JAN-17 07:00, 24-JAN-17 17:00)
Let (s2, e2) be defined as (24-JAN-17 16:30, 24-JAN-17 17:30)
Result: TRUE
(which is correct) 结果: TRUE
(正确)
Case 2 : 情况2 :
Let (s1, e1) be defined as (24-JAN-17 07:00, 24-JAN-17 17:00)
Let (s2, e2) be defined as (24-JAN-17 16:30, 25-JAN-17 07:00)
Result: TRUE
(which is correct) 结果: TRUE
(正确)
Case 3 : 情况3 :
Let (s1, e1) be defined as (24-JAN-17 07:00, 24-JAN-17 17:00)
Let (s2, e2) be defined as (24-JAN-17 17:00, 25-JAN-17 07:30)
Result: FALSE
(which is INCORRECT) 结果: FALSE
(不正确)
It is my understanding that it does this because (in case of #3 above), it overflows into the next day... but if that is the case, why does it work for Case 2
? 据我了解,这样做是因为(在上述#3的情况下)它溢出到第二天……但是,如果是这样,为什么它对Case 2
? (is it because the overlap happens on the same day, contrary to Case 3
?) (是因为重叠发生在同一天,与Case 3
相反?)
Regardless, my question is, how to tweak my expression, such that it covers Case 3, above? 无论如何,我的问题是,如何调整我的表情,使其涵盖上述案例3?
Any and all help appreciated! 任何和所有帮助表示赞赏! Thank you! 谢谢!
I have already reviewed the following threads, but to no avail: 我已经检查了以下线程,但无济于事:
Determine Whether Two Date Ranges Overlap 确定两个日期范围是否重叠
Determining if two time ranges overlap at any point (Specifically: https://stackoverflow.com/a/13387860/7458905 ) 确定两个时间范围是否在任何时间点重叠 (具体是: https : //stackoverflow.com/a/13387860/7458905 )
The reason that your last case doesn't return TRUE is due to equality. 您的最后一种情况不返回TRUE的原因是由于相等。
You say in case 3: 您说情况三:
Let (s1, e1) be defined as (24-JAN-17 07:00, 24-JAN-17 17:00)
Let (s2, e2) be defined as (24-JAN-17 17:00, 25-JAN-17 07:30)
Which means that e1
is equal to s2
(they are both 24-JAN-17 17:00) 这意味着e1
等于 s2
(它们都是17年1月24日17:00)
But your logic is only true if s2 < e1
, which is NOT true in this case. 但是,只有当s2 < e1
,您的逻辑才是正确的,在这种情况下是不正确的。
Modify your logic so that it returns true if they are equal: 修改您的逻辑,以使其在相等时返回true:
$isOverlapping = (s1 <= e2 && s2 <= e1)
You could use some simple math to do this one, too. 您也可以使用一些简单的数学来完成这一操作。
Here we simply break down the components into their unix timestamps and then use the dx/dy of both to determine if they overlap 在这里,我们只是将组件分解为unix时间戳,然后使用两者的dx / dy来确定它们是否重叠
<?php
function is_overlapping($date_range_1, $date_range_2) {
// convert into unix timestamp (milliseconds since Jan 1st 1970)
$d1_1 = make_timestamp($date_range_1[0]);
$d1_2 = make_timestamp($date_range_1[1]);
$d2_1 = make_timestamp($date_range_2[0]);
$d2_2 = make_timestamp($date_range_2[1]);
// grab the delta of both ranges
$delta2 = $d2_1 - $d1_2;
$delta1 = $d2_2 - $d1_1;
// if either has a delta < 0 then they overlap
if (($delta2 < 0) || ($delta1 < 0)) { return "true"; }
// catchall return value
return "false";
}
// convert to unix timestamp function
function make_timestamp($inp_date) {
return strtotime($inp_date);
}
// test cases
print_r("Case 1: " .
is_overlapping(array("24-JAN-17 07:00", "24-JAN-17 17:00"),
array("24-JAN-17 16:30", "25-JAN-17 17:30"))
);
print_r("<br>");
print_r("Case 3: " .
is_overlapping(array("24-JAN-17 07:00", "24-JAN-17 17:00"),
array("24-JAN-17 17:30", "25-JAN-17 18:30"))
);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.