Home > Backend Development > PHP Tutorial > How Can I Determine the First and Last Day of the Week in PHP?

How Can I Determine the First and Last Day of the Week in PHP?

Mary-Kate Olsen
Release: 2024-12-08 03:36:09
Original
631 people have browsed it

How Can I Determine the First and Last Day of the Week in PHP?

Determining the First Day of the Week in PHP

To retrieve the first day of the week given a date in the format MM-dd-yyyy, follow the steps below:

Step 1: Determine the Current Day of the Week

Use the date() function to get the current day of the week as a number from 0 to 6, where Sunday is represented by 0 and Monday by 1.

$day = date('w');
Copy after login

Step 2: Calculate Day of Previous Sunday

Subtract the current day of the week from the current date to get the date for the previous Sunday.

$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
Copy after login

Step 3: Calculate Day of Subsequent Saturday

Add 6 days to the current date to get the date for the subsequent Saturday.

$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));
Copy after login

Output:

The variables $week_start and $week_end now contain the date for Sunday and Saturday of the current week in MM-dd-YYYY format, respectively.

The above is the detailed content of How Can I Determine the First and Last Day of the Week in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template