I do not know how to check if datetime variable is today, tomorrow or yesterday.
I did not find a method in the class members.
To check if a date is yesterday:Subtract 1 day from the current date to get yesterday's date.
Explanation: In the above example, first of all, we find tomorrow's date by adding today's date with the timespan of 1. Here, we get today's date using the DateTime. Now method.
Description: To get yesterday's date in PHP simply use the DateTime class and instantiate it with the constructor parameter 'yesterday' Then you can format the DateTime object with the format method.
final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); final yesterday = DateTime(now.year, now.month, now.day - 1); final tomorrow = DateTime(now.year, now.month, now.day + 1); final dateToCheck = ... final aDate = DateTime(dateToCheck.year, dateToCheck.month, dateToCheck.day); if(aDate == today) { ... } else if(aDate == yesterday) { ... } else(aDate == tomorrow) { ... }
Hit: now.day - 1
and now.day + 1
works well with dates that result in a different year or month.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With