Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Restrict of overlap of schedule PHP

I have a PHP code and it work my only problem is if i create a schedule that overlaps the previous schedule

for example i schedule

**7:00 am to 8:30 am ** Succesfully Added

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

**6:30am to 7:30am ** Error Message: The schedule is conflict with other schedules

**6:30 am to 9:00 am ** Successfully Added (this one overlap a schedule so this needs to be a error)

**7:30am to 9:30am ** Error Message: The schedule is conflict with other schedules

This is the sample of my code

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

>Solution :

You have not covered the case where d_start<datetime_start AND d_end>datetime_end

Change the SELECT to include this:

...
('{$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)
 )
...

EDIT:
A simpler structure is this:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading