用php中的时隙检查时间的可用性

用php中的时隙检查时间的可用性,php,datetime,timeslots,Php,Datetime,Timeslots,我想用Php和mysql每半小时显示一次商店可用性状态 为此,我尝试使用以下代码为我创建时间段 $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-

我想用Php和mysql每半小时显示一次商店可用性状态

为此,我尝试使用以下代码为我创建时间段

$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;
            }
        }
        print_R($time);
我想要这样的结果

Array
(
    [1] => Array
        (
            [start] => 10:00
            [end] => 10:30
            [status] => availiable
        )

    [2] => Array
        (
            [start] => 10:30
            [end] => 11:00
            [status] => booked
        )
这是我在phpmyadmin的预订表

id       shop_id     date        booking_start_time    booking_close_time
1        3           4-7-2019        10:00                 11:00

我该怎么做?提前感谢

您需要比较数据库中的时隙,如下所示:

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;
    }
    //Here you need to write query and fetch data.
    $todayDate = date('d-m-Y'); //Please check date format. It should be similar as your database date field format.
    //please use data binding instead of contacting variable.
    $selectQuery = "select `id` from `booking` where date = "'.$todayDate.'" and 
        (( `booking_start_time` >= "'.$start.'" AND `booking_start_time` <= "'.$start.'" ) || 
        (`booking_close_time` >= "'.$end.'" AND `booking_close_time` <= "'.$end.'")) ";

    // After, you need to exeucte this query and need to check query output. if it has records, then you need to show booked else available. as below
    $result = mysqli_query($con, $selectQuery);
    if ($result->num_rows) {
        $time[$i]['status'] = 'booked';
    } else {
        $time[$i]['status'] = 'availiable';
    }
}
print_R($time);
while(strotime($start\u time)num\u行){
$time[$i]['status']='booked';
}否则{
$time[$i]['status']='available';
}
}
打印(时间);

希望对您有所帮助。

预订表中是否有日期字段?预订取决于日期吗?@Rohitmital:是的,预订取决于日期和时间,但您的预订表没有日期字段?如何获得日期栏?@Rohitmital:现在请检查我更新了我的问题,请确认它对你有帮助,干杯!!
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;
    }
    //Here you need to write query and fetch data.
    $todayDate = date('d-m-Y'); //Please check date format. It should be similar as your database date field format.
    //please use data binding instead of contacting variable.
    $selectQuery = "select `id` from `booking` where date = "'.$todayDate.'" and 
        (( `booking_start_time` >= "'.$start.'" AND `booking_start_time` <= "'.$start.'" ) || 
        (`booking_close_time` >= "'.$end.'" AND `booking_close_time` <= "'.$end.'")) ";

    // After, you need to exeucte this query and need to check query output. if it has records, then you need to show booked else available. as below
    $result = mysqli_query($con, $selectQuery);
    if ($result->num_rows) {
        $time[$i]['status'] = 'booked';
    } else {
        $time[$i]['status'] = 'availiable';
    }
}
print_R($time);