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

InspiredPrynce's avatar

How to check if a date is today or yesterday

Hello guys, i need to check if a datetime is either yesterday or today in laravel5.

0 likes
10 replies
tykus's avatar

Using Carbon:

$date = Carbon::parse($dt);

$isToday = $date->isToday();

$isYesterday = $date->isYesterday();

$isTodayOrYesterday =  $date->isToday() || $date->isYesterday();
2 likes
InspiredPrynce's avatar

I am passing a datetime like this "2019-10-02 09:21:52"

Your answer keeps returning false on both queries

tykus's avatar

What is the application's timezone, and how does that relate to your timezone?

Also, what is your server's system time?

1 like
Nakov's avatar

Here is my tinker test, and it works perfectly well:

>>> Carbon\Carbon::parse("2019-10-02 09:21:52")
=> Carbon\Carbon @1570008112 {#3031
     date: 2019-10-02 09:21:52.0 UTC (+00:00),
   }
>>> $date = Carbon\Carbon::parse("2019-10-02 09:21:52")
=> Carbon\Carbon @1570008112 {#3034
     date: 2019-10-02 09:21:52.0 UTC (+00:00),
   }
>>> $date->isToday()
=> true
>>> $date->isYesterday()
=> false
>>> 
1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Can you show your code? I just did this and got 1 (true)

    $date = Carbon::parse("2019-10-02 09:21:52");

    return $date->isToday() ? 1 : 0;
5 likes
tykus's avatar

If your server's system time is incorrect/different timezone, you will be testing against that time, rather than your real local time.

1 like
Sinnbeck's avatar

You can try using now(); to see the current system time.

InspiredPrynce's avatar

Thanks alot guys, i appreciate you all...

It was working alright.... I was passing the wrong time in my controller...

warpig's avatar

i think diffForHumans starts showing the date after 3 weeks has passed by, I would like to print the date after a week passed, is there a method for this?

Snapey's avatar

@warpig why drag up a 3 year old solved question for something unrelated?

Please or to participate in this conversation.