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.
Using Carbon:
$date = Carbon::parse($dt);
$isToday = $date->isToday();
$isYesterday = $date->isYesterday();
$isTodayOrYesterday = $date->isToday() || $date->isYesterday();
I am passing a datetime like this "2019-10-02 09:21:52"
Your answer keeps returning false on both queries
What is the application's timezone, and how does that relate to your timezone?
Also, what is your server's system time?
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
>>>
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;
If your server's system time is incorrect/different timezone, you will be testing against that time, rather than your real local time.
You can try using now(); to see the current system time.
Thanks alot guys, i appreciate you all...
It was working alright....
I was passing the wrong time in my controller...
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?
@warpig why drag up a 3 year old solved question for something unrelated?
Please sign in or create an account to participate in this conversation.