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

标签 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)它仍然会让我预订空间,因为一个两项正在进行的事件与要求的日期不重叠

我希望你能帮我解决这个问题。谢谢

最佳答案

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

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";
}

关于php - 在时隙之间查找具有给定任务的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680224/

相关文章:

php - CSS-检查并在 li 有 ul 的地方添加右箭头

php - 使用 PHP 和 Guzzle 流式传输远程文件

php - array to string 转换错误,需要先 explode 再内爆

php - 在谷歌云上获取 502 错误网关

php - 当 MYSQL 没有数据时 PHP 出现错误消息?

php postgres pdo 获取和选择查询

php - 每个 php 都有多个 post 值

mysql - 优化sql查询

python - 内爆列表以在 python MySQLDB IN 子句中使用

mysql - 在mysql中使用内连接语法更新表