Home >Backend Development >PHP Tutorial >How to Get the Dates of a Specific Week Given a Week Number and Year in PHP?
How to Calculate Days of the Week Given a Week Number
Calculating the specific days within a given week number, starting from Monday, requires a specific approach. Let's delve into the solutions:
PHP Solution:
Given a week number ($week_number) and year ($year), you can determine the dates within that week as follows:
<code class="php">for ($day = 1; $day <= 7; $day++) { echo date('m/d/Y', strtotime($year . "W" . $week_number . $day)) . "\n"; }</code>
For example, with $week_number = 40 and $year = 2008, this code will output:
10/06/2008 10/07/2008 10/08/2008 10/09/2008 10/10/2008 10/11/2008 10/12/2008
Alternative PHP Solution (Given a Date String):
To obtain the days of a week starting from Monday given a specific date, you can use the week_from_monday() function:
<code class="php">function week_from_monday($date) { [...] // (See detailed code in the original article) } $dates = week_from_monday('07-10-2008'); print_r($dates); // Outputs the array of dates for that week</code>
This solution provides an array of dates starting from the Monday of the specified week, making it a useful utility for date manipulation tasks.
The above is the detailed content of How to Get the Dates of a Specific Week Given a Week Number and Year in PHP?. For more information, please follow other related articles on the PHP Chinese website!