Reputation: 5048
I have a web app written in Laravel 3. In this application one of the models is "Activities". These activities have both a start date and end date field. I was wondering what the best way is to query all Activities that start AND end the same day?
-- Update --
I'm using mySQL and the field types are both Timestamp...
What I have currently:
...
$activitySchedule = ActivitySchedule::with(array('location'))->where(function($query) {
// Query activities that start and end today
$yesterday = date('Y-m-d', strtotime('today')) . ' 00:00:00';
$tomorrow = date('Y-m-d', strtotime('today')) . ' 23:59:59';
$query->where('starts', '>', $yesterday);
$query->where('ends', '<', $tomorrow);
});
...
I think this works properly but I'm wondering if there is a more precise method?
Upvotes: 1
Views: 558
Reputation: 49
You should be using Carbon package which would allow you to get startOftheDay
and endoftheday
like this:
$StartofDay = Carbon::now()->startOfDay();
$EndofDay = Carbon::now()->endOfDay();
Once you have this in place, query your model with a where condition involving a condition to check using StartofDay
and EndofDay
values along with created_at
column.
Find carbon package here : https://github.com/briannesbitt/Carbon
Hope it helps.!!
Upvotes: 1