Home > Backend Development > PHP Tutorial > How Can I Find the First Day of the Week Given a Date in PHP?

How Can I Find the First Day of the Week Given a Date in PHP?

Patricia Arquette
Release: 2024-12-11 19:18:13
Original
537 people have browsed it

How Can I Find the First Day of the Week Given a Date in PHP?

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'));
Copy after login

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!

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