PHP: How to check if a date is today, yesterday or tomorrow
The core issue in your logic stems from how strtotime() interacts with relative time strings when passed a base timestamp versus an absolute one. In your snippet, you are comparing $match_date (which represents the parsed input string) against calculations derived from $date (the current system time). The fundamental problem lies in this line:
elseif(strtotime("-1 day", $date) == $match_date) {
//Yesterday
}
When you pass a second timestamp as the second argument to strtotime(), it calculates a new timestamp relative to that specific point in time. However, your $match_date is formatted as a string using date('d.m.Y H:i'). You are then comparing this formatted string directly against the return value of strtotime(), which returns an integer Unix timestamp (seconds since epoch). A string equality check between a date-time string and an integer will invariably fail, causing your code to consistently drop into the else block.
To fix this, you need to ensure that both sides of your comparison are in the same formatâideally, integers representing Unix timestamps. This removes ambiguity regarding timezones and formatting inconsistencies. Here is a corrected approach using strtotime() for all operations:
$timestamp = "2014-09-02T13:34"; // Note: ISO 8601 format is generally safer than d.m.Y
$currentTimestamp = strtotime('now');
$inputTimestamp = strtotime($timestamp);
if ($inputTimestamp == $currentTimestamp) {
echo "Today";
} elseif ($inputTimestamp == $currentTimestamp - 86400) {
// Subtracting 24 hours (86400 seconds) for yesterday
echo "Yesterday";
} elseif ($inputTimestamp == $currentTimestamp + 86400) {
// Adding 24 hours for tomorrow
echo "Tomorrow";
} else {
echo "Sometime";
}
This method is robust because it treats dates as raw time values. It handles the transition across midnight correctly without relying on string formatting logic, which can break if your server clock differs from the client or input source.
However, there is a significant trade-off to consider: granularity. If you only care about the calendar date (e.g., "Is this the 2nd of September?"), comparing full timestamps including hours and minutes might be overly strict. If your user submits a form at 13:34 but the server checks at 05:00, they are technically different timestamps, even though it is still "today."
In scenarios where you want to ignore the time component, it is often cleaner to normalize everything to midnight UTC or the local timezone before comparison. You can use strtotime() combined with date('Y-m-d') to strip the time:
$targetDate = date('Y-m-d', strtotime($timestamp));
$today = date('Y-m-d');
if ($targetDate === $today) {
// Today
} elseif ($targetDate === date('Y-m-d', strtotime('-1 day'))) {
// Yesterday
} elseif ($targetDate === date('Y-m-d', strtotime('+1 day'))) {
// Tomorrow
}
This approach reduces the risk of false negatives caused by slight time discrepancies. While Laravel's Carbon package offers fluent interfaces for this exact logic, sticking to native PHP functions ensures compatibility even if you are not using a framework. If you are building on top of Laravel or similar ecosystems, leveraging their date helpers can simplify these checks further, but understanding the underlying timestamp mechanics remains essential for debugging edge cases.
Remember that strtotime is powerful but can sometimes be ambiguous with non-standard formats. Always validate input strings against known patterns before passing them to time functions to prevent unexpected parsing errors that might push a valid "today" date into your else block.
