Reputation: 3
I have a uploaded_at
column in database having type as timestamp.
$from = date("d-m-Y", strtotime("-2 months"));
$data = Post::where('uploaded_at' , '>' , $from)->paginate(20);
This piece of code is giving all the data from the post table where I needed only the last 2 months post.
Upvotes: 0
Views: 129
Reputation: 568
Try this code
$from = date("Y-m-d", strtotime("-2 months"));
$data = Post::whereDate('uploaded_at' , '>=' , $from)->paginate(20);
Upvotes: 1
Reputation: 34688
Laravel has a default package, named Carbon
Which can easily handle do this, like this :
$date = Carbon::now();
$previous = $date->subMonths(2);
$data =Post::whereBetween('uploaded_at', [$previous, $date])->paginate(20);
Don't forget to use Carbon\Carbon;
on th top.
Hope this helps..
Upvotes: 1
Reputation: 1296
Use the whereDate()
function to compare date:
$data = Post::whereDate('uploaded_at' , '>' , $from)->paginate(20);
Upvotes: 0