Sylar
Sylar

Reputation: 12082

Get data created x minutes ago in Laravel

Is there a query, with PHP date or Carbon to get data created x minutes ago?

In my example, I need all data created 3 minutes ago. If the time is 10:00 and data was created, then at when query is made 10:03, I should get all data created at 10:00. If query made 10:04, no data show be returned.

Edit per answer: To return all data created between 3 minutes ago and now.

use App/Foo
use Carbon\Carbon;

[...]

$three_minutes_ago = Carbon::now()->subMinutes(3)->toDateTimeString();
$foo = Foo::where('created_at', '<', $three_minutes_ago)->get(); // Is this correct?

$foo is always empty no matter data is in Foo. I've used tinker to create/save data,

Upvotes: 8

Views: 9132

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

Just tested and this is a working solution:

Foo::whereBetween('created_at', [now()->subMinutes(3), now()])->get()

Upvotes: 21

Related Questions