【问题标题】:How to check if two times overlap in PHP?如何检查PHP中的两次是否重叠?
【发布时间】:2015-05-30 13:25:17
【问题描述】:

所以我想弄清楚如何使用 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;
}

应该可以正常工作的AFAIK,所以我去检查时间是否重叠并将任何重叠的时间添加到数组中:

$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>";
}

但是,当我打印出 $clashes 数组的条目时,我将其作为输出:

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

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

数据库信息:

Date 是 Date SQL 类型,Time 是 Time SQL 类型,Length 是 double(8,2) SQL 类型

$userBands 信息:

$userBands 变量是 Show 模型的 Laravel 集合

【问题讨论】:

  • 您能否提供有关您选择的数据类型的mysql(我假设)数据到列datetimelength?我怀疑 phpMyAdmin 在 php 脚本读取数据时没有显示数据,这将有很大帮助...
  • $userBands 变量是 Show 模型的 Laravel 集合。
  • Date是Date SQL类型,Time是Time SQL类型,Length是double(8,2) SQL类型。

标签: php time compare unix-timestamp


【解决方案1】:

首先: 您可以更改 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;
}

但您必须考虑 4 种不同的情况:

所以如果一个事件包含另一个事件,你会找到它。

最好的!

【讨论】:

    【解决方案2】:

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

    新版本:

    function testRange($s1, $e1, $s2, $e2) {
        return $s1 <= $e2 && $s2 <= $e1;
    }
    

    但不知道为什么这比前一个有效。

    【讨论】:

      猜你喜欢
      • 2012-06-21
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      相关资源
      最近更新 更多