Cowgirl
Cowgirl

Reputation: 1494

Laravel query for a range of time

I am looking for a way to make a query which compares time, morning - 6 to 9, day - 9 to 12 , evening 12 - 5

Right now, I am using an select option in html

<select name="time" id="time">
          <option disabled value="">Choose A Time</option>
          <option value="06:00 - 09:00">Morning - 06:00 - 09:00 </option>
          <option value="10:00 - 13:00">Day - 10:00 - 13:00 </option>
          <option value="14:00 - 18:00">Evening - 14:00 - 18:00</option>
        </select>

And I am trying to query it like this, datetime I get it from the database and it's like 2017-12-13 06:15:00

    $query->whereTime('datetime', '>=', $request->get('time').':00')                    
    });

I have no idea how I can compare a range of time, like compare 06:00-09:00 with 06:15

Upvotes: 5

Views: 18386

Answers (1)

Sandeep
Sandeep

Reputation: 21144

You can make use of the same query as above to find the table rows between two times.

$query->whereTime('timestamp', '>=', \Carbon\Carbon::parse('06:00'))
     ->whereTime('timestamp', '<=', \Carbon\Carbon::parse('06:30'))

Dont concatenate date string like this,

$request->get('time').':00')  

This is not quite appropriate, and you might fail with this approach.

Upvotes: 13

Related Questions