Get the First Day of the Week in PHP
If you have a date in the MM-dd-yyyy format and want to determine the first day of the week, here's a code snippet that can help:
$day = date('w'); $week_start = date('m-d-Y', strtotime('-'.$day.' days')); $week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));
This code utilizes the PHP function date to extract the day of the week as an integer ranging from 0 to 6, where 0 represents Sunday and 6 represents Saturday.
Using this day value, it calculates the dates for the first and last days of the week. The result is stored in the variables $week_start and $week_end, respectively.
Therefore, $week_start will contain the date for Sunday of the current week, while $week_end will hold the date for the Saturday of the current week, all in the MM-dd-yyyy format.
The above is the detailed content of How Can I Find the First Day of the Week Given a Date in PHP?. For more information, please follow other related articles on the PHP Chinese website!