-1

我有一个用户输入表单,其中用户选择(YYYY-mm-dd)格式的日期,(H:i)的开始时间和(H:i)格式的结束时间,或者假设我有三个具有以下值的变量.

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

我现在必须将此值与现有的值数组进行比较。我现有的数组看起来像这样。

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
        )

)

我必须检查选择的用户输入[日期、开始时间和结束时间]是否在给定的时隙数组中已经预订了任何时隙。从我看到的解决方案是首先拆分并匹配日期,如果它与日期匹配,则需要相应地匹配时间。我对如何使用它感到困惑。我希望从你们那里得到一些简单的解决方案:)

谢谢你。

更新: 我试过这样做。

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

它不工作。

4

4 回答 4

3

假设用户日期/时间的格式如下:

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

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

以这种方式查找重叠:

$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

这基本上采取了这样的非重叠条件:

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

并简单地交换条件。

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

于 2012-11-22T12:47:27.260 回答
1

检查一下...并根据您的要求对其进行修改以匹配所有现有时间...我只为您匹配了一个...

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

$existing[] = 
        array
        (
            'id' => 1,
            'start_datetime' => '2012-11-22 02:30:00',
            'end_datetime' => '2012-11-22 05:00:00'
        );

$UserStartTime = strtotime($date." ".$startTime);
$UserEndTime = strtotime($date." ".$endTime);

$ExistingStartTime = strtotime($existing[0]['start_datetime']);
$ExistingEndTime = strtotime($existing[0]['end_datetime']);

if($UserStartTime >= $ExistingStartTime && $UserEndTime <= $ExistingEndTime)
{
    echo "BOOKED";
}
else
{
    echo "NOT BOOKED";
}
于 2012-11-22T12:34:05.817 回答
1

由于现有数组来自 SQL 查询:

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

if(strlen($startTime==4)){$startTime="0".$startTime;}
if(strlen($endTime==4)){$endTime="0".$startTime;}

$query="SELECT id FROM table WHERE start_datetime='$date $startTime:00' OR end_datetime='$date $endtimeTime:00'";

如果查询返回某些内容,则id返回已预订!

如果您想知道任何数据库信息是否与两个日期时间匹配,只需将 替换ORAND.

如果您想知道用户时间段是否在数据库的任何条目之间:

$query="SELECT id FROM table WHERE (start_datetime>='$date $startTime:00' AND start_datetime<='$date $endTime:00') OR (end_datetime<='$date $starttimeTime:00' AND end_datetime>='$date $endtimeTime:00') OR (start_datetime<='$date $startTime:00' AND end_datetime>='$date $starttimeTime:00')";
于 2012-11-22T12:31:06.750 回答
0

这可能会帮助你

$date = '2012-11-22';
$startTime= '1:00:00'; // padded two zero
$endTime = '4:00:00'; // padded two zero

$start_datetime = $date. " ".(strlen(intval(substr($startTime,0,2))) > 1 ? $startTime : "0".$startTime);

$end_datetime = $date. " ".(strlen(intval(substr($endTime,0,2))) > 1? $endTime : "0".$endTime);
for($i = 0;$i<count($your_array);$i++)
{
    if($your_array[$i]['start_datetime'] == $start_datetime)
    {
        // Start date Found
    }
    if($your_array[$i]['end_datetime'] == $end_datetime)
    {
        // Start end Found
    }
}
于 2012-11-22T12:39:16.047 回答