11

我想看看我从数据库读取的时间是否与用户提供的时间重叠。

我的数据库如下所示:

-----------------------------------------------
|organiser|meeting_start|meeting_end|boardroom|
-----------------------------------------------
| John Doe| 1340193600  | 1340195400| big     |
-----------------------------------------------

我的代码如下所示:

date_default_timezone_set('Africa/Johannesburg');
$from = strtotime($_GET['meeting_date'] . ' ' . $_GET['meeting_start']);
$to = strtotime($_GET['meeting_date'] . ' ' . $_GET['meeting_end']);
$another_meeting = false;
$meeting_date = strtotime($_GET['meeting_date']);
$meeting_next = $meeting_date + 86400;

$result = mysql_query("SELECT meeting_start, meeting_end FROM admin_boardroom_booking WHERE boardroom = '" . $_GET['boardroom'] . "' AND meeting_start >= '" . $meeting_date . "' AND meeting_end < '" . $meeting_next . "'")or die(mysql_error());
while($row = mysql_fetch_array($result)) {
    $from_compare = $row['meeting_start'];
    $to_compare = $row['meeting_end'];

    $intersect = min($to, $to_compare) - max($from, $from_compare);
    if ( $intersect < 0 )
        $intersect = 0;

    $overlap = $intersect / 3600;
    if ( $overlap <= 0 ) {
        $another_meeting = true;
        break;
    }
}

if ($another_meeting)
    echo 'ERROR';

如果我故意键入两个重叠时间,它不会回显错误。我究竟做错了什么?

4

6 回答 6

33

当且仅当至少满足以下条件之一时,两个时间段 P1 和 P2 重叠:

  1. P1 在 P2 的开始和结束之间开始 ( P2.from <= P1.from <= P2.to)
  2. P2 在 P1 的开始和结束之间开始 ( P1.from <= P2.from <= P1.to)

这将捕获部分重叠的时期以及一个完全覆盖另一个时期的时期。如果它们重叠,则其中一个句点必须始终在另一个句点内开始(或结束)。

所以$another_meeting将定义为:

$another_meeting = ($from >= $from_compare && $from <= $to_compare) ||
                   ($from_compare >= $from && $from_compare <= $to);

<如果一个事件可以在与另一事件完全相同的时间开始,您可能希望将边界情况更改为严格检查。

于 2012-06-19T10:21:08.097 回答
4

只是在做类似的事情......但只是随着时间......

$startTime = strtotime("7:00");
$endTime   = strtotime("10:30");

$chkStartTime = strtotime("10:00");
$chkEndTime   = strtotime("12:10");

if($chkStartTime > $startTime && $chkEndTime < $endTime)
{
    // Check time is in between start and end time
    echo "1 Time is in between start and end time";
}
elseif(($chkStartTime > $startTime && $chkStartTime < $endTime) || ($chkEndTime > $startTime && $chkEndTime < $endTime))
{
    // Check start or end time is in between start and end time
    echo "2 ChK start or end Time is in between start and end time";
}
elseif($chkStartTime==$startTime || $chkEndTime==$endTime)
{
    // Check start or end time is at the border of start and end time
    echo "3 ChK start or end Time is at the border of start and end time";
}
elseif($startTime > $chkStartTime && $endTime < $chkEndTime)
{
    // start and end time is in between  the check start and end time.
    echo "4 start and end Time is overlapping  chk start and end time";
}
于 2012-06-19T10:08:49.920 回答
2

我可能会用这样的方法解决它:

function avaliable($start, $end) {
  // checks if there's a meeting between start or end
  $q = "SELECT * FROM admin_boardroom_booking "
    . "WHERE NOT (meeting_start BETWEEN '$end' AND '$start' "
    . "OR meeting_end BETWEEN '$end' AND '$start')";
  $result = mysql_query($q);

  // returns true on no conflicts and false elseway
  return mysql_num_rows($result) === 0;
}
于 2012-06-19T10:44:40.083 回答
0

Emil Vikström 的回答是正确的,但需要考虑一种情况。
就像,其中一个时间范围是另一个时间范围的子集。
因此,假设P1{start_time, end_time}P2{start_time, end_time}将在以下任何一项为真时重叠。

  • P1.start_time <= P2.start_time <= P1.end_time
  • P1.start_time <= P2.end_time <= P1.end_time
  • P2.start_time <= P1.start_time <= P1.end_time <= P2.end_time

只是假设时间按升序排序。下面的例子:

|-----------------------------------|
|  Start time  |   End time  | Name |
|-----------------------------------|
|    10:00     |    14:00    |  P1  |
|-----------------------------------|
|    12:00     |    16:00    |  P2  |
|-----------------------------------|
|    08:00     |    12:00    |  P3  |
|-----------------------------------|
|    07:00     |    16:00    |  P4  |
|-----------------------------------|

如果您将 P1 视为基准时间,并且您想对照它检查 P2、P3、P4。

  1. P1.start_time <= P2.start_time <= P1.end_timetrue
  2. P1.start_time <= P3.end_time <= P1.end_timetrue
  3. P4.start_time <= P1.start_time <= P1.end_time <= P4.end_timetrue

这是您可以检查任何时间是否与另一个时间重叠的方法。

于 2017-10-29T05:39:40.687 回答
0

如果 $to 总是晚于 $from 我们可以使用这个更短的解决方案

$another_meeting = !($from > $to_compare || $from_compare > $to);
于 2018-11-27T17:15:43.673 回答
0

我处理边缘情况的解决方案非常紧凑:

    private function meetingOverlap($meeting1, $meeting2){
        if ($meeting1['start']>$meeting2['start'] && $meeting1['start']<$meeting2['end'] ) return true;
        if ($meeting2['start']>$meeting1['start'] && $meeting2['start']<$meeting1['end'] ) return true;
        if ($meeting2['start']==$meeting2['start'] && $meeting1['end']==$meeting2['end'] ) return true;
        return false;
    }
于 2022-02-13T20:27:10.727 回答