Marco Santos
Marco Santos

Reputation: 41

Retrieving date time between a period

i have a list of records, in my case are matches, and each match have a "date_time" that the game starts, i need to get all the matches that shows 1 hour before it starts and stays on the page after 5 hours. But i believe im not doing right, is not working right

code:

$liveMatches = SoccerMatch::where('date_time','>',Carbon::now()->subHour())
            ->where('date_time', '<',Carbon::now()->addHours(5))
            ->where('status',1)
            ->get();

Upvotes: 1

Views: 695

Answers (1)

aaron0207
aaron0207

Reputation: 2333

You can use the ->whereBetween() method

Try the following:

$liveMatches = SoccerMatch::whereBetween('date_time',[Carbon::now()->subHours(5)->subSeconds(5),Carbon::now()->addHour()->addSeconds(5)])
            ->where('status',1)
            ->get();

Check Query Builder docs and let me know if it worked

Upvotes: 1

Related Questions