Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data created x minutes ago in Laravel

Tags:

php

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,

like image 448
Sylar Avatar asked Jan 02 '18 10:01

Sylar


1 Answers

Just tested and this is a working solution:

Foo::whereBetween('created_at', [now()->subMinutes(3), now()])->get()
like image 181
Alexey Mezenin Avatar answered Oct 15 '22 10:10

Alexey Mezenin