Date validation based on time period in Laravel 5
I have a table booked
with columns start
and end
like datestamps;
I know what is it
$start = "2015-07-12 01:00:00";
$end = "2015-07-12 02:00:00";
$users = DB::table('booked')
->whereBetween('start', [$start, $end])->get();
checks for any dates in the column start
that fall within $ start and $ end.
What I want is actually a different way.
I am having difficulty checking if the $ start and $ end date variables appear in the columns start
and end
in the table book
.
source to share
// make sure $from is before $till, because they are probably provided as input
$from = min($start, $end);
$till = max($start, $end);
// then simply
DB::table('booked')
->where('start', '<=', $from)
->where('end', '>=', $till)
->get();
It will return rows corresponding to the period $from-$till
contained in the period start-end
:
start end
|------------------------|
|----------------|
$from $till
source to share