How to Calculate the Days of the Week Given a Week Number or Date in PHP?

Linda Hamilton
Release: 2024-10-26 09:20:02
Original
457 people have browsed it

How to Calculate the Days of the Week Given a Week Number or Date in PHP?

Calculating Days of the Week Given a Week Number

Given a week number, such as the one obtained using the date -u %W command, it is often useful to generate the corresponding dates for the days of that week. Let's start with an example.

Consider week number 40 of the year 2008. Using ISO 8601's rules for week numbering, we expect the days in that week to be:

2008-10-06
2008-10-07
2008-10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12
Copy after login

PHP Solution:

In PHP, the date function provides various formatting options. Here's a simple loop that prints the dates for the days of a given week number:

<code class="php">$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
    echo date('Y-m-d', strtotime($year."W".$week_number.$day))."\n";
}
Copy after login

This loop iterates through the 7 days of the week and prints the corresponding dates in the format: YYYY-MM-DD.

PHP Solution for Calculating Days of a Week from a Given Date:

A different scenario involves calculating the days of a week given a specific date. Here's a PHP function that does this:

<code class="php">function week_from_monday($date) {
    // Extract date parts
    list($day, $month, $year) = explode("-", $date);

    // Get the weekday of the given date
    $wkday = date('l',mktime('0','0','0', $month, $day, $year));

    // Calculate the number of days to subtract to get Monday
    switch($wkday) {
        case 'Monday': $numDaysToMon = 0; break;
        case 'Tuesday': $numDaysToMon = 1; break;
        case 'Wednesday': $numDaysToMon = 2; break;
        case 'Thursday': $numDaysToMon = 3; break;
        case 'Friday': $numDaysToMon = 4; break;
        case 'Saturday': $numDaysToMon = 5; break;
        case 'Sunday': $numDaysToMon = 6; break;   
    }

    // Get timestamp of Monday
    $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

    // Calculate dates for 7 days from Monday
    $dates = array();
    $seconds_in_a_day = 86400;
    for($i=0; $i<7; $i++)
    {
        $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
    }

    return $dates;
}
Copy after login

Example usage:

<code class="php">$dates = week_from_monday('07-10-2008');
print_r($dates);
Copy after login

Output:

Array
(
    [0] => 2008-10-06
    [1] => 2008-10-07
    [2] => 2008-10-08
    [3] => 2008-10-09
    [4] => 2008-10-10
    [5] => 2008-10-11
    [6] => 2008-10-12
)</code>
Copy after login

The above is the detailed content of How to Calculate the Days of the Week Given a Week Number or 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!