Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查PHP中的两次重叠?_Php_Time_Compare_Unix Timestamp - Fatal编程技术网

如何检查PHP中的两次重叠?

如何检查PHP中的两次重叠?,php,time,compare,unix-timestamp,Php,Time,Compare,Unix Timestamp,因此,我试图找出如何使用PHP和MySQL数据库检查两次重叠 这是我用来检查它们是否重叠的函数: function testRange($start_time1,$end_time1,$start_time2,$end_time2) { return ($end_time1 < $start_time2) ? false : ($start_time1 > $start_time2) && ($start_time1 > $end_time2) ? f

因此,我试图找出如何使用PHP和MySQL数据库检查两次重叠

这是我用来检查它们是否重叠的函数:

function testRange($start_time1,$end_time1,$start_time2,$end_time2) {
    return ($end_time1 < $start_time2) ? false : ($start_time1 > $start_time2) && ($start_time1 > $end_time2) ?  false : true;
}
函数测试范围($start\u time1、$end\u time1、$start\u time2、$end\u time2){
返回($end_time1<$start_time2)?false:($start_time1>$start_time2)和($start_time1>$end_time2)?false:true;
}
好的,应该可以正常工作,所以我去检查时间是否重叠,并将任何重叠的时间添加到数组中:

$clashes = [];
$idTracker = [];
foreach ($userBands as $show) {
    foreach ($userBands as $show1) {
        if(!($show->id == $show1->id)) {
            if(strcmp($show->date, $show1->date) == 0 && testRange(strtotime($show->time), strtotime($show->time) + ($show->length*60), strtotime($show1->time), strtotime($show1->time) + ($show1->length*60))) {
                array_push($clashes, [
                    "s1_id" => $show->id,
                    "s2_id" => $show1->id
                ]);
            }
        }
    }
}

foreach ($clashes as $clash) {
    foreach ($clashes as $clash1) {                  
        //If Clash1->ID1 == Clash2->ID1 then
    }
    print_r($clash);
    echo "<br>";
}
$collists=[];
$idTracker=[];
foreach($userBands作为$show){
foreach($userBands作为$show1){
如果(!($show->id==$show1->id)){
如果(strcmp($show->date,$show1->date)==0&&testRange(strotime($show->time),strotime($show->time)+($show->length*60),strotime($show1->time),strotime($show1->time)+($show1->length*60))){
数组_推送($碰撞[
“s1_id”=>$show->id,
“s2_id”=>$show1->id
]);
}
}
}
}
foreach($clash作为$clash的冲突){
foreach($clash1){
//如果Clash1->ID1==Clash2->ID1,则
}
打印(冲突);
回声“
”; }
但是,当我打印$CLUSTS数组的条目时,我会将其作为输出:

这完全是错误的,因为这是数据库的内容:

27和26不应该与25冲突,但它们被追踪到$Clamples数组中。为什么会这样

数据库信息:


日期是日期SQL类型,时间是时间SQL类型,长度是双精度(8,2)SQL类型

$userBands信息:


$userBands变量是Show模型的一个larvel集合

通过更改用于比较时间的算法来修复它

新版本:

function testRange($s1, $e1, $s2, $e2) {
    return $s1 <= $e2 && $s2 <= $e1;
}
函数测试范围($s1、$e1、$s2、$e2){
首先返回$s1:
您可以更改SQL查询以获取结束时间,如下所示:

SELECT *, 
ADDTIME(
  CONCAT(time,'.0'), 
  CONCAT(CONCAT(CONCAT( length DIV 60, ':' ), length MOD 60) ,':0.0') 
) AS EndTime 
FROM `table`;
现在有了一个名为EndTime的列

因此,此代码可以工作:

if(strcmp($show->date, $show1->date) == 0 &&
   newTestRange(
      strtotime($show->time), 
      strtotime($show->EndTime), 
      strtotime($show1->time), 
      strtotime($show1->EndTime) 
      ) ) {
我不知道为什么会这样:

function newTestRange($start_time1,$end_time1,$start_time2,$end_time2) {
 //                   12:30:00    13:20:00   12:45:00     13:45:00
// return ($end_time1 < $start_time2) ? false : ($start_time1 > $start_time2) && ($start_time1 > $end_time2) ?  false : true;
    $ret = false; // its all okay.
    if ($start_time1 < $start_time2 ){
        if ($end_time1 > $start_time2 ){
            $ret = true;echo "\n case1-$start_time1-colide-$start_time2 \n";
        }
    } else if ($end_time2 > $start_time1){
        $ret = true; echo "\n case2-$start_time1-colide-$start_time2 \n";
    }
    return $ret;
}
函数newtedrange($start\u time1、$end\u time1、$start\u time2、$end\u time2){
//                   12:30:00    13:20:00   12:45:00     13:45:00
//返回($end_time1<$start_time2)?false:($start_time1>$start_time2)和($start_time1>$end_time2)?false:true;
$ret=false;//一切正常。
如果($start\u time1<$start\u time2){
如果($end_time1>start_time2){
$ret=true;回显“\n案例1-$start\u time1-colide-$start\u time2\n”;
}
}else if($end_time2>$start_time1){
$ret=true;回显“\n案例2-$start\u time1-colide-$start\u time2\n”;
}
返回$ret;
}
但是你必须考虑4种不同的情况:

因此,如果一个事件包含另一个事件,您将找到它


最好的!

你能提供mysql吗(我假设)关于您选择的列
日期
时间
长度
的数据类型的数据?我怀疑,phpMyAdmin不会在php脚本读取数据时显示数据……$userBands变量是Show模型的一个Laravel集合。date是日期SQL类型,time是时间SQL类型,而长度是双精度(8,2)SQL类型。