1

I have this function:

public function get_free_courtrooms() {
        $post = file_get_contents('php://input');
        $_POST = json_decode($post, true);

        $date = $this->input->post('date', true);
        $time_start = $this->input->post('time_start', true);
        $time_end = $this->input->post('time_end', true);

        $where = array('date' => $date);
        $reservations = $this->MY_Model->get('reservations', $where);

        $courtrooms = $this->MY_Model->get('courtrooms');

        $output = array(
            'free_courtrooms' => $free_courtrooms
        );

        echo json_encode($output);
        exit();
    }

I need to remove all courtrooms from $courtrooms that are booked in sending hours ($time_start, $time_end).

In reservations table I have:

  • id
  • courtroom_id
  • date
  • time_start
  • time_end
3
  • Whaat the hell are post hours??? Commented May 29, 2019 at 17:52
  • $time_start and $time_end Commented May 29, 2019 at 17:55
  • Just add a where clause conditioning $time_start, $time_end. Commented May 29, 2019 at 19:23

1 Answer 1

2

I would start with simple function to check overlapping 2 time:

function isOverlap($s1, $e1, $s2, $e2) {
    return (($s1 > $s2 && $s1 < $e2) || ($e1 > $s2 && $e1 < $e2)
            || ($s1 < $s2 && $e1 > $2) || ($s1 > $s2 && $e1 < $e2));
}

This will be visualization of the 4 cases:

---     |    ---  |   -------   |     ---
  ---   |   ---   |     ---     |   -------

Now just fill array of all busy rooms:

foreach($reservations as $res) {
    if (isOverlap($time_start, $time_end, $res["start"], $res["end"]))
        $busy_courtrooms[] = $res["id"]; 
}

Now calculate the diff between all the $courtrooms and the $busy_courtrooms using array-diff:

$free_courtrooms = array_diff($courtrooms, $busy_courtrooms);

And you done

I don't now how your $reservations array is build but I guess it has "start" and "end" attribute...

Sign up to request clarification or add additional context in comments.

1 Comment

But I must return from this function list of all courtrooms that do not have a reservation. $free_courtrooms is all courtrooms that are not reservation during these hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.