【发布时间】: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(我假设)数据到列
date、time和length?我怀疑 phpMyAdmin 在 php 脚本读取数据时没有显示数据,这将有很大帮助... -
$userBands 变量是 Show 模型的 Laravel 集合。
-
Date是Date SQL类型,Time是Time SQL类型,Length是double(8,2) SQL类型。
标签: php time compare unix-timestamp