What is the best way to determine the first Monday of a specific date in PHP?
To determine the first Monday of a specific date in PHP, you can use the `strtotime` function to find the timestamp of the given date and then calculate the offset to the next Monday. By subtracting the day of the week from the timestamp and adding 1 if the day is after Monday, you can find the timestamp of the first Monday of that week.
$date = "2022-01-15"; // Specific date
$timestamp = strtotime($date);
$firstMonday = strtotime('last Monday', $timestamp);
if (date('N', $timestamp) > 1) {
$firstMonday = strtotime('+1 week', $firstMonday);
}
echo date('Y-m-d', $firstMonday); // Output: 2022-01-10
Related Questions
- How can the provided PHP code snippet be modified to improve email validation while accounting for the limitations of checkdnsrr function on Windows platforms?
- How can arrays in sessions be used to track progress and manage steps in a multi-step installation process in PHP?
- What are the best practices for handling cURL requests in a production environment, especially in terms of security and performance?