我有一个用户输入表单,其中用户选择(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';
}
}
}
它不工作。