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

.

+3


source to share


2 answers


// 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

      

+4


source


The following query will give you all records for which both start and end are between $ start and $ end :



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

      

0


source







All Articles