This PHP code snippet provides a convenient way to determine the start and end days of the current week based on a user-specified first day. By setting the `$week_start_day` variable (0 for Sunday, 1 for Monday), the code calculates the beginning and end of the week, considering the current date and time. The resulting DateTime objects, represented by `$this_week_first_day` and `$this_week_last_day`, are then modified to reflect the precise start and end times of the week.
Source code viewer
// PHP Code for Determining Start and End Days of the Week Based on a Specified First Day // Define the starting day of the week (0 for Sunday, 1 for Monday) $week_start_day = 0; // Create DateTime objects for the current date and time $date_time_from = new DateTime('now'); $date_time_to = clone $date_time_from; // Calculate the first day of the current week $this_week_first_day = $date_time_from->setISODate( $date_time_from->format('Y'), $date_time_from->format('W'), $week_start_day ); // Calculate the last day of the current week $this_week_last_day = $date_time_to->setISODate( $date_time_to->format('Y'), $date_time_to->format('W'), (6 + $week_start_day) ); // Set the time to the start of the day and end of the day, respectively $this_week_first_day->modify('00:00:00.000000'); $this_week_last_day->modify('23:59:59.999999'); // Now, $this_week_first_day and $this_week_last_day represent the start and end of the current week.Programming Language: PHP