Reputation: 2661
On my website, I use Laravel's created_at
to store the time at which images are uploaded.
However, I want to be able to create a query that gets images only within a specific time frame.
Those would be:
-The past 24 hours
-The past week
-The past month
-The path year
I was thinking of something like...
$images = Images::where('created_at', '>=', Carbon::now()->hours(24))->get();
It would be easy to get records from within specific hours, since I could simply go from 24 to 168 to 672 to 2087 (day, week, month, year).
Thoughts?
Upvotes: 2
Views: 600
Reputation: 163968
Use sub
methods:
Images::where('created_at', '>=', Carbon::now()->subDay())->get();
Images::where('created_at', '>=', Carbon::now()->subWeek())->get();
Images::where('created_at', '>=', Carbon::now()->subMonth())->get();
Images::where('created_at', '>=', Carbon::now()->subYear())->get();
Or:
Images::where('created_at', '>=', Carbon::now()->subHours(24))->get();
Upvotes: 2