Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ronster's avatar

Mysql query question

Hi,

I'm having difficulties writing a query (in eloquent) but also without.

I am hosting livescores for soccer matches. I want to return all the matches that are currently being played. I have a field called starting_at with holds a datetime with starting date/time of the match. I want to show the matches 30 minutes before they begin and 30 minutes after they have stopped. you can assume that a match takes no longer then 3 hours (extratime/penalty shootout etc)

When using interval I, at some point, are missing matches since the starting_at is before now-interval 30 minutes.

Is there a mysql guru here that can help me with this?

thanks

0 likes
2 replies
tomopongrac's avatar

@ Ronster

Try this:

$matches = Match::where('starting_at', '>', Carbon::now()->subMinutes(30)
    ->where('starting_at', '<', Carbon::now()->addMinutes(210))
    ->get();
Ronster's avatar

That will never works since time changes and games that are being played are not shown anymore.

I believe this may work

->whereRaw( 'starting_at + interval 220 minute between NOW() - interval 30 minute AND NOW() + interval 4 hour')

Please or to participate in this conversation.