在时隙之间查找具有给定任务的空间

时间:2014-12-28 20:19:02

标签: php mysql

这就是我需要的:

打印在特定时间段内具有可用性的员工的ID,例如:from 28-12-2014 10:00 to 28-12-2014 11:00 根据这张图片,这些员工可能正在进行BD上的其他一些活动:Table of ongoing activities

我有什么:

目前我正在将这些日期与我需要的日期进行比较,如下所示:

$sql_reservas="SELECT * FROM `Reservas` WHERE `ID_Empleado`= 2";
        $result_reservas = mysqli_query($link,$sql_reservas);    
        if(mysqli_num_rows($result_reservas)>0){        
        foreach($result_reservas as $row_reservas ) {          
        ongoing_start       = new DateTime("{$row_reservas['Inicio']}"); //28-12-2014 11:45 then 28-12-2014 09:45
        ongoing_finish      = new DateTime("{$row_reservas['Libre']}"); //28-12-2014 13:45 then 28-12-2014 10:45
        new_activity_start  = $_POST['date'] //28-12-2014 10:00
        new_activity_finish = $_POST['date2'] //28-12-2014 11:00
        If(new_activity_start<ongoing_start && new_activity_finish<ongoing_start ){
        //I have space before the ongoing activities
        echo $row_reservas['ID_Empleado'];
        }elseif(new_activity_start>ongoing_finish && new_activity_finish>ongoing_finish) {
        //I have space after the ongoing activities
       echo $row_reservas['ID_Empleado'];       
       }else{
        //overlaps with the compared one
        echo "not available";

        }}}

问题:

当只有一个预订(如ID 3或4或5 ......)时一切正常,但是当有2个或更多预订时(如ID 2),它仍然会让我预订空间,因为一个正在进行的两项活动与所要求的日期不重叠

我希望你能帮助我。感谢

1 个答案:

答案 0 :(得分:0)

你需要改变循环和回声的顺序

available = true;
foreach ($result_reservas as $row_reservas ) {          
    ongoing_start       = new DateTime("{$row_reservas['Inicio']}"); //28-12-2014 11:45 then 28-12-2014 09:45
    ongoing_finish      = new DateTime("{$row_reservas['Libre']}"); //28-12-2014 13:45 then 28-12-2014 10:45
    new_activity_start  = $_POST['date'] //28-12-2014 10:00
    new_activity_finish = $_POST['date2'] //28-12-2014 11:00
    if (new_activity_start<ongoing_start && new_activity_finish<ongoing_start  || 
        new_activity_start>ongoing_finish && new_activity_finish>ongoing_finish) {
    //I have space after the ongoing activities
        $id = $row_reservas['ID_Empleado'];       
    } else {
        available = false;
        break;
    }
}
if (available) {
    echo $id;
} else {
    echo "not available";
}