我正在制作事件日历,当用户预订时间时,其他用户将无法选择该时间以及接下来的两个时隙 例如我们有3:00 AM,3:30 AM,4:00 AM到24小时,如果用户选择3:00 AM,则也将禁用3:30 AM和4:00 AM。
我已禁用了3:00 AM,但我没有找到一种方法来动态禁用3:30 AM和4:00 AM广告位。
$date = '20190713';
$taken_slots = array();
$bookings_qry = $wpdb->get_results("SELECT booked_end_time FROM wp_booked_time WHERE booked_date=$date");
foreach($bookings_qry as $bookings_row)
$taken_slots[] = $bookings_row->booked_end_time;
$slots_qry = $wpdb->get_results("SELECT * FROM wp_time");
$calendar = '<select>';
$calendar .= '<option value="select" disabled="disabled">Select a Time</option>';
foreach($slots_qry as $slots_row){
$slot_id = $slots_row->time_id;
$calendar .= '<option value="'.$slot_id.'"';
if(in_array($slot_id, $taken_slots))
$calendar .= 'disabled="disabled"';
$calendar .= '>';
$calendar.= $slots_row->times.'</option>';
}
$calendar .= '</select>';
echo $calendar;
在上面的代码中,我正在从wp_time表中获取时间,并从数据库中匹配wp_booked_time表中的预订值。现在请帮助我如何禁用下两个值。任何帮助将不胜感激。预先感谢
答案 0 :(得分:0)
操纵时间(并遍历时间间隔)的最佳方法是使用本机PHP DateTime,DateInterval和DatePeriod。您需要检查当前单位间隔是否与现有约会重叠。下一步将是检查选定的时隙是否足够长,以达到选定/所需的单位数量。有进一步定制的空间,但是(我希望)应该为您指明正确的方向。
<?php
/* Example "Office" or entity business rules (possibly loaded from DB?) */
$officeOpenTime = '08:00:00'; // 8am open
$officeCloseTime = '17:00:00'; // 5pm close
$officeMinPerUnit = 15; //Appt unit is 15 minutes long
$officeApptMinUnits = 2; //Appt minumum duration is 2 units (30 minutes)
/* Convert office minPerUnit to more useful DateInterval */
$unitInterval = new \DateInterval('PT'.$officeMinPerUnit.'M');
/* Day selected [presumably] from input */
$curSelectedDate = '2019-07-09';
/* Convert office open and close times to usable DateTime */
$curSelectedDayOfficeOpenDatetime = new \DateTime($curSelectedDate.' '.$officeOpenTime);
$curSelectedDayOfficeCloseDatetime = new \DateTime($curSelectedDate.' '.$officeCloseTime);
/* Iterable period per unit */
$curSelectedDayUnits = new \DatePeriod($curSelectedDayOfficeOpenDatetime, $unitInterval, $curSelectedDayOfficeCloseDatetime);
/* Current bookings test dataset- represents data from DB (make sure to ALWAYS order by datetime ascending)
* Example (using open/close times):
* SELECT * FROM appointments WHERE start_datetime < '2019-07-09 17:00:00' AND end_datetime > '2019-07-09 08:00:00' ORDER BY start_datetime ASC
*/
$testAppts = [
[
'start_datetime' => '2019-07-09 08:00:00',
'end_datetime' => '2019-07-09 09:00:00'
],
[
'start_datetime' => '2019-07-09 11:00:00',
'end_datetime' => '2019-07-09 11:30:00'
],
[
'start_datetime' => '2019-07-09 14:00:00',
'end_datetime' => '2019-07-09 15:00:00'
],
[
'start_datetime' => '2019-07-09 15:00:00',
'end_datetime' => '2019-07-09 16:00:00'
],
[
'start_datetime' => '2019-07-09 16:00:00',
'end_datetime' => '2019-07-09 17:00:00'
]
];
?>
<html>
<head>
</head>
<body>
<form>
<select>
<option value="" disabled selected>Select a time</option>
<?php
$apptCurIndex = 0;
$numAppts = count($testAppts);
/* Convert first appt times to usable DateTime if exists */
if( $numAppts > 0 ){
$curApptStartDatetime = new \DateTime($testAppts[$apptCurIndex]['start_datetime']);
$curApptEndDatetime = new \DateTime($testAppts[$apptCurIndex]['end_datetime']);
}
foreach( $curSelectedDayUnits as $unitStartDatetime){
echo '<option value="'.$unitStartDatetime->format('Y-m-d H:i:s').'"';
/* Does slot need checking? */
if( $numAppts > 0 ){
/* Does slot need to be locked? */
$unitEndDatetime = (clone $unitStartDatetime)->add($unitInterval);
if( $unitEndDatetime > $curApptStartDatetime ){
echo ' disabled';
}
/* Does current appt need to be incremented */
if( $apptCurIndex < $numAppts - 1 && $unitEndDatetime >= $curApptEndDatetime ){
$apptCurIndex++;
$curApptStartDatetime = new \DateTime($testAppts[$apptCurIndex]['start_datetime']);
$curApptEndDatetime = new \DateTime($testAppts[$apptCurIndex]['end_datetime']);
}
}
echo '>'.$unitStartDatetime->format('g:i a').'</option>';
}