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
Mary-Kate OlsenOriginal
2024-12-08 03:36:09614browse

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');

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'));

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'));

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!

Statement:
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