日程重叠限制PHP [duplicate]

wgx48brx  于 2022-12-17  发布在  PHP
关注(0)|答案(1)|浏览(87)

此问题在此处已有答案

What's the most efficient way to test if two ranges overlap?(16个答案)
2天前关闭。
我有一个PHP代码,它的工作我唯一的问题是,如果我创建一个时间表,重叠以前的时间表
例如,i计划

上午7:00至上午8:30添加成功
上午6:30至上午7:30错误消息:该计划与其他计划冲突
****6:30 am到9:00 am已成功添加(此操作与计划重叠,因此这必须是错误)
上午7:30到9:30错误消息:该计划与其他计划冲突

这是我的代码示例

$d_start = strtotime($datetime_start);
            $d_end = strtotime($datetime_end);
            $roomID = $assembly_hall_id;

            $chk = $this->conn->query("SELECT * FROM `schedule_list` where (('{$d_start}'   
            Between unix_timestamp(datetime_start) and unix_timestamp(datetime_end)) or ('{$d_end}' 
            Between unix_timestamp(datetime_start) and unix_timestamp(datetime_end))) ".(($roomID > 0) ? "
            and assembly_hall_id ='{$roomID}' and sched_status = '1' " : ""))->num_rows;
            
            if($chk > 0 ){
                $resp['status'] = 'failed';
                $resp['err_msg'] = "The schedule is conflict with other schedules.";
            }elseif(strtotime($datetime_start) == null)
            {   
                $resp['status'] = 'failed';
                $resp['err_msg'] = "Date and Time Schedule is Invalid.";
            }
9rygscc1

9rygscc11#

您尚未介绍d_start〈日期时间_start AND d_end〉日期时间_end的情况
更改SELECT以包括以下内容:

...
('{$d_start}' Between unix_timestamp(datetime_start) and unix_timestamp(datetime_end))
 OR
 ('{$d_end}' Between unix_timestamp(datetime_start) and unix_timestamp(datetime_end))
 OR
 (
  '{$d_start}' < unix_timestamp(datetime_start)
  AND
  '{$d_end}' > unix_timestamp(datetime_end)
 )
...

编辑:一个更简单的结构是:

...
('{$d_start}' < unix_timestamp(datetime_end)
 AND  
 '{$d_end}' > unix_timestamp(datetime_start)) 
...

相关问题