我有一个“预订”表,其中包含商店详细信息(存储了开始时间,关闭时间),用于预订/预约。
我想每30分钟创建一个时隙,并希望每个时隙检查商店的可用性状态。
这是一张表格“预订”:
id shop_id booking_date start_time end_time
1 3 15-7-2019 10:01 11:30
现在我得到以下结果。还应该预订第一个时隙(在查询中不起作用之间)
Array
(
[1] => Array
(
[start] => 10:00
[end] => 10:30
[status] => availiable
)
[2] => Array
(
[start] => 10:30
[end] => 11:00
[status] => booked
)
[3] => Array
(
[start] => 11:00
[end] => 11:30
[status] => booked
)
}
...
这是我的代码:
$duration="30";
$start="10:00AM";
$end="07:00PM";
$start = new DateTime($start);
$end = new DateTime($end);
$start_time = $start->format('H:i');
$end_time = $end->format('H:i');
$i=0;
while(strtotime($start_time) <= strtotime($end_time)){
$start = $start_time;
$end = date('H:i',strtotime('+'.$duration.' minutes',strtotime($start_time)));
$start_time = date('H:i',strtotime('+'.$duration.' minutes',strtotime($start_time)));
$i++;
if(strtotime($start_time) <= strtotime($end_time)){
$time[$i]['start'] = $start;
$time[$i]['end'] = $end;
}
$todayDate = date('d-m-Y');
if(strtotime($start_time) <= strtotime($end_time)){
$time[$i]['start'] = $start;
$time[$i]['end'] = $end;
}
$query=$this->db->query("SELECT `id` FROM `booking` WHERE booking_date = '$todayDate' AND
( `start_time` <= CAST('$start' AS TIME) AND `end_time` >= CAST('$end' AS TIME) )");
if ( $query->num_rows() > 0 )
{
$rows = $query->result_array();
$time[$i]['status'] = 'booked';
}
else
{
$rows = $query->result_array();
$time[$i]['status'] = 'availiable';
}
}
print_r($time);