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

Laracast13's avatar

get post via subdays

Hello How get post after 2 days it was created

Post::where('created_at', '>', (new \Carbon\Carbon)->subdays(2) )

for ex. if post created in 1 June at I want get it with ORM when day is 3 June

0 likes
10 replies
Nakov's avatar

So instead of new instance of Carbon, just use helpers from today:

Post::whereDate('created_at', '>', today()->subDays(2))->get();
Laracast13's avatar

@Nakov

Post::whereDate('created_at', '>', today()->subDays(2))->get();

I need this for cron job so, if I use this condition it will show post which was created at 1 June for. ex. 2 June - showing 3 June- showing 4 June- showing I want show it after 2 days (not in 1 and 2 June)

Sinnbeck's avatar

@www888 So you want it to be on exactly 2 days exactly?

Post::whereDate('created_at', today()->subDays(2))->get();
1 like
Laracast13's avatar

@Sinnbeck

I will explain again.

Need get Post which was created 2 Day ago and after created has passed.

For ex. Post was created_at 1.06.2022

Post::whereDate('created_at', today()->subDays(2))->get();

If I run this in 2.06.2022 it don't give me this post If I run this in 3.06.2022 it don't give me this post If I run this in 4.06.2022 it give me this post

Sinnbeck's avatar

@www888 And that code should do exactly that. Add a new row in your database and set created at to 2 days ago, and run the query

Please or to participate in this conversation.