404error
404error

Reputation: 575

Selecting records that fall withing a time range in MySQL with Laravel 4

Hello everyone I am having a problem I cant figure out. I have a database with a couple of columns. title, body, start, and end.

The start and end columns are of the datetime type. I have rows in my database with start and end dates.

I am trying to select all rows that fall within a date range. In this case a 24 hour span.

So for example, if i have a row that has a start day of today(Sept. 24, 2013) and an end date of Jan. 1, 2014, I expect it to be returned.

Route::get('/', function()
{
//Start of Day
$start = date('Y-m-d 00:00:00');

//End of Day    
$end = date('Y-m-d 23:59:59'); 

$data['flyers'] = Flyer::where('start', '>=', $start)->where('end', '<=', $end)->get();

//return homepage
return View::make('view', $data);
});

Thanks in advance. I will post my answer if I figure it out first.

Upvotes: 0

Views: 1308

Answers (1)

Adam Goforth
Adam Goforth

Reputation: 46

The comparison operators on your where() calls are backwards. The way your code is now, you are only selecting Flyers that start and end today. What you want is this:

$data['flyers'] = Flyer::where('start', '<=', $start)->where('end', '>=', $end)->get();

Upvotes: 3

Related Questions