我将如何检测两个时间段一个非重叠? [英] how would I detect a non-overlap of two time periods?

查看:170
本文介绍了我将如何检测两个时间段一个非重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入的形式,其中用户选择(YYYY-MM-DD)格式的日期,开始时间:在时间和结束时间(H I)(H:我)格式,或假设我有三个变量用以下值

I have a user input form where user select the date in (YYYY-mm-dd) format, and start time in (H:i) and end time in (H:i) format, or assume i have three variable with following values.

$date      = '2012-11-22';
$startTime = '1:00';
$endTime   = '4:00';

我现在要比较这值与值的现有阵列。我现有的阵列是这样的。

i now have to compare this values with existing array of values. my existing array look like this.

Array
(
    [0] => Array
        (
            [id] => 1
            [start_datetime] => 2012-11-22 02:30:00
            [end_datetime] => 2012-11-22 05:00:00
        )

    [1] => Array
        (
            [id] => 2
            [start_datetime] => 2012-11-22 00:00:00
            [end_datetime] => 2012-11-22 02:30:00
        )

    [2] => Array
        (
            [id] => 3
            [start_datetime] => 2012-11-22 05:00:00
            [end_datetime] => 2012-11-22 08:00:00
        )

    [3] => Array
        (
            [id] => 4
            [start_datetime] => 2012-11-22 08:00:00
            [end_datetime] => 2012-11-22 11:00:00
        )

    [4] => Array
        (
            [id] => 5
            [start_datetime] => 2012-11-22 11:00:00
            [end_datetime] => 2012-11-22 17:00:00
        )

)

我要检查,如果用户输入所选的 [日期的startTime,和endTime] 有任何时隙的定数组中已经预订的插槽。从我看到的解决办法是先拆搭配的日期,如果日期匹配,则时间需要进行相应的匹配。我感到困惑的是如何去用它。我希望能得到一些简单的解决方案,从你们:)

i have to check if the user input selected [date, startTime, and endTime] has any of the slots already booked in the given array of time slots. from what i see the solution is to first split and match the date, if it matches the date then time needs to be matched accordingly. i am confused about how to go with it. i hope to get some simple solution from you guys :)

感谢你。

更新: 我试图做这样的。

$arenaSurfaceBooking = array(
    array(
        'id' => 1,
        'start_datetime' => '2012-11-22 02:30:00',
        'end_datetime' => '2012-11-22 05:00:00',
    ),
    array(
        'id' => 2,
        'start_datetime' => '2012-11-22 00:00:00',
        'end_datetime' => '2012-11-22 02:30:00',
    ),
    array(
        'id' => 3,
        'start_datetime' => '2012-11-22 05:00:00',
        'end_datetime' => '2012-11-22 08:00:00',
    ),
    array(
        'id' => 4,
        'start_datetime' => '2012-11-22 08:00:00',
        'end_datetime' => '2012-11-22 11:00:00',
    ),
    array(
        'id' => 5,
        'start_datetime' => '2012-11-22 11:00:00',
        'end_datetime' => '2012-11-22 17:00:00',
    )
);

foreach($arenaSurfaceBooking as $booking) {
    $bookedDateTimestamp = strtotime(date('Y-m-d', strtotime($booking['start_datetime'])));
    $inputDateTimestamp  = strtotime($context['date']);
    $bookedStartTime     = strtotime(date('H:i', strtotime($booking['start_datetime'])));
    $bookedEndTime       = strtotime(date('H:i', strtotime($booking['end_datetime'])));
    $userStartTime       = strtotime(date('H:i', strtotime($context['start_datetime'])));
    $userEndTime         = strtotime(date('H:i', strtotime($context['end_datetime'])));
    if( $bookedDateTimestamp == $inputDateTimestamp) {
        if( $userEndTime <= $bookedStartTime || $userStartTime >= $bookedEndTime) {
            echo 'i am booked';
        }
    }
}

它不工作。

推荐答案

让我们假设用户日期/时间的格式如下所示:

Let's assume the user date/time is formatted like so:

$date      = '2012-11-22';
$startTime = '01:00'; // note the extra '0' in front
$endTime   = '04:00';

$userStartTime = "$date $startTime";
$userEndTime = "$date $endTime";

查找重叠做是这样的:

Finding overlaps is done this way:

$overlaps = 0;
foreach ($block as $block) {
    if ($userStartTime < $block['end_datetime'] && $userEndTime > $block['start_datetime']) {
        ++$overlaps;
    }
}

// if $overlaps > 0 -> there's a booking in the way

这基本上是采取了非重叠情况是这样的:

This is basically taking a non-overlap condition like this:

$userEndTime <= $block['start_datetime'] || $userStartTime >= $block['end_datetime']

和简单的交换条件。

这可以很容易地写进SQL以及减少数据库和应用程序之间的网络开销。

This can easily be written into SQL as well to reduce the network overhead between database and application.

这篇关于我将如何检测两个时间段一个非重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆