0

I have something like this:

$from = min($start_date, $end_date);
$till = max($start_date, $end_date);

DB::table('booked')
     ->where('start', '<=', $from)
     ->where('end', '>=', $till)
     ->get();

This is a nice solution form stackoverflow.

It seems works, but I need the opposite.

In my DB I have this data: start: 2020-09-27 10:00:00 end: 2020-09-27 12:00:00 I have to query that rows where for example:

$start_date(2020-09-27 9:00:00) and $end_date(2020-09-27 11:00:00) where both not in start: and end: Hopefully my question is clear enough.

7
  • your end_date 2020-09-27 11:00:00 is between the given range and your telling in opposite way Commented Sep 25, 2020 at 21:37
  • Yes this is the point, if it is between the given range then I don't want it in the result. Only if not in the given range. Commented Sep 25, 2020 at 21:47
  • so it would be like, if any parameter start or end date is between given date range, you don't want to include Commented Sep 25, 2020 at 21:53
  • Yes exactly, just the given date range is stored in the db, and the other range is variable. So I would like to check that the variable is not in the stored range. Commented Sep 25, 2020 at 22:01
  • 1
    Just to be clear. Let's say data in db are start - 10, end = 12 and now if input range is start = 8, end = 9 => yes, start = 9, end = 11 => no, start = 11, end = 11:30 => no, start = 11, end = 13 => no, start = 13, end = 14 = yes. If that so, will give you a query Commented Sep 25, 2020 at 22:04

1 Answer 1

1

If I am getting you right this is what you need

$from = min($start_date, $end_date);
$till = max($start_date, $end_date);

DB::table('booked')
     ->where('start', '>=', $till)
     ->orWhere('end', '<=', $from)
     ->get();
Sign up to request clarification or add additional context in comments.

4 Comments

It assumes you have correct(logically) data in database.
Hmm, this is great! Thank you very much, I just work on this on the last 2 hours and I can't understand why not working properly! (Probably I need some rest) Cheers!
This is really works well, but I have a problem. If I have more date in the database then it not working anymore, any idea?
What you mean by "more date"? What is not working? Please, describe.

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.