Get date for Monday and Friday for the current week
Calculating the start and end of a work week requires more than just subtracting or adding days. The core issue often lies in how we define the "current" day relative to the calendar week. When the current day is Sunday (day 1) or Saturday (day 7), standard arithmetic logic that assumes a Monday-to-Sunday flow breaks down, resulting in incorrect dates for the upcoming Friday or previous Monday.
Consider the provided snippet:
$current_day = date("N");
$days_to_friday = 5 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("Y-m-d", strtotime("- {$days_from_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_to_friday} Days"));
The logic here attempts to calculate the offset from today. If today is Monday (current_day = 1), $days_to_friday becomes 4, which correctly points to Friday. However, if today is Sunday (current_day = 1), the calculation for Friday yields 0 days, suggesting today is Friday. Since Sunday is not Friday, this logic fails immediately on Sundays. Similarly, Saturday (current_day = 7) results in a negative offset for Monday, pushing the date back incorrectly into the prior week's calculations or failing to identify the correct start of the current work week.
The fundamental flaw is treating the calendar as a continuous sequence rather than discrete weeks anchored to specific days. To fix this, we must normalize the day index so that Monday always represents day 0 or 1 consistently, regardless of whether today falls in the middle, at the start, or at the very end of the week.
A robust approach involves determining the number of days until the next Friday and the number of days since the current Monday. Since date("N") returns a value between 1 and 7 (Monday being 1), we can adjust the offset calculation to handle the wrap-around case where the current day is after Friday.
Here is a corrected implementation that accounts for all seven days:
$current_day = (int) date("N"); // Returns 1 (Mon) through 7 (Sun)
// Calculate days until next Friday
// If today is Friday, we want 0. If today is Saturday, we want -1 (previous week's Friday).
// The formula (5 - $current_day + 7) % 7 handles the wrap-around logic cleanly.
$days_to_friday = (5 - $current_day + 7) % 7;
// Calculate days since current Monday
// If today is Monday, we want 0. If today is Tuesday, we want 1.
// The formula ($current_day - 1) works for Mon-Sun, but needs adjustment if we consider
// that the "current" Monday might be in the past if today is Saturday or Sunday.
$days_since_monday = ($current_day - 1);
$monday = date("Y-m-d", strtotime("- {$days_since_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_to_friday} Days"));
Wait, the previous logic still has a subtle edge case regarding Saturday and Sunday. On Saturday ($current_day = 7), $days_to_friday calculates to (5 - 7 + 7) % 7 = 5. This is incorrect; on Saturday, the next Friday was yesterday (or today if we consider the week just ending). However, in most business contexts, "Friday of this week" usually refers to the upcoming Friday if today is before Friday, or the current Friday if today is Friday.
Let's refine the logic to strictly target the start and end of the current calendar week as defined by Monday through Sunday. The standard definition is that a week starts on Monday. Therefore:
- If today is Saturday or Sunday, the "current" Monday is in the past.
- If today is Friday, the "current" Friday is today.
The modulo operator % is your best friend here for handling cyclic date arithmetic. To find the days until the next (or current) Friday relative to a Monday start:
$current_day = (int) date("N"); // 1=Mon, ..., 7=Sun
// Days from Monday to today (0 if Mon, 6 if Sun)
$offset_from_monday = ($current_day - 1);
// Days from today to next Friday (relative to Monday start of week)
// We want the day index within the week where Friday is 4.
$days_until_friday = 4 - $offset_from_monday;
if ($days_until_friday < 0) {
$days_until_friday += 7; // Wrap around to next week if we passed Friday
}
$monday = date("Y-m-d", strtotime("- {$offset_from_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_until_friday} Days"));
This approach normalizes the day index to a 0-6 range where Monday is 0 and Friday is 4. By calculating the difference between the current offset and the target offset (Friday), we get the precise number of days to shift. If the result is negative (meaning we are past Friday in the current week, i.e., Saturday or Sunday), adding 7 pushes us to the start of the following week, effectively finding the next Friday.
This method ensures that whether you are running this script on a Tuesday or a Sunday, you receive the correct Monday and Friday dates for the current business week. It avoids hardcoding specific conditions like if ($current_day == 7), reducing code complexity while maintaining accuracy across all seven days of the week.