The Edge Case of strtotime("last Monday") on Mondays
When working with relative date strings in PHP, it is easy to assume that logic works linearly without hitting boundary conditions. A common pattern involves calculating a past week's start date using strtotime("last Monday"). This works perfectly fine for Tuesday through Sunday, but the behavior changes subtly when the current day happens to be Monday.
If you execute this function on a Monday, PHP returns the timestamp for the Monday of the previous week rather than today's date. This occurs because the parser interprets "last Monday" as strictly "the most recent Monday in the past," excluding the current moment if it is already that day. For many applications, returning yesterday's start of the week when you are currently on the weekend is acceptable. However, for scheduling systems or recurring tasks, this off-by-one-week error can cause logic failures where a job meant to run today is delayed by seven days.
To handle this edge case without hardcoding dates or checking day names manually inside your logic, you must adjust the reference point before calculating the relative string. The most robust approach involves shifting the current date back one day first. By treating "yesterday" as the anchor point, strtotime("next Monday") will naturally resolve to today if it is currently Monday. If today is Tuesday through Sunday, that same expression correctly points to next week.
Here is a practical implementation using standard PHP functions:
$today = new DateTime();
// If today is Monday, we want today.
// If today is Tuesday-Sunday, we want last Monday.
// By going back 1 day, Monday becomes Sunday, and "next Monday" targets today.
// Any other day becomes a day where "next Monday" targets next week or last week appropriately.
$targetDate = $today->modify('-1 day');
$targetDate->modify('next Monday');
echo $targetDate->format('Y-m-d');
Alternatively, you can use the DateTime object's built-in navigation capabilities to avoid string parsing ambiguity entirely. You can explicitly check the current weekday and conditionally add or subtract intervals. This method offers more transparency regarding what is happening under the hood.
$today = new DateTime();
if ($today->format('N') === '1') {
// N format returns 1 for Monday.
// If we are on Monday, we want this Monday.
$targetDate = clone $today;
} else {
// Otherwise, calculate last Monday.
$targetDate = new DateTime('last Monday');
}
While the conditional approach is explicit and easy to debug, it does introduce a branching logic that some prefer to avoid in favor of pure arithmetic or string manipulation. The -1 day trick effectively converts the "today" scenario into a "past week" scenario for the parser, unifying the logic into a single expression.
It is worth noting that strtotime behavior can vary slightly depending on the PHP version and the underlying C library implementation, particularly regarding timezone handling. Always ensure your DateTime object is aware or explicitly set to a specific timezone before performing calculations if your application operates across regions. For projects requiring strict date consistency without relying on string parsing quirks, consider using Laravel's Carbon library, which wraps these native PHP classes and often provides helper methods like startOfWeek() that handle these relative logic puzzles gracefully.
Understanding how the parser resolves relative terms is crucial for building reliable scheduling systems. What appears to be a simple one-liner hides an assumption about whether "last" includes the present moment. By adjusting the reference date, you align the parser's interpretation with your specific business requirement, ensuring that Monday schedules never slip into next week unintentionally.