Home > Backend Development > PHP Tutorial > PHP Date and Time Application 9: Get the start and end dates of a certain week in a certain year

PHP Date and Time Application 9: Get the start and end dates of a certain week in a certain year

藏色散人
Release: 2023-04-10 14:46:01
Original
4673 people have browsed it

Today we will continue to introduce the series of articles on PHP date and time application, so the article "PHP date and time application summary (continuously updated~)" will also be updated in time, and interested friends can collect it. Study~

If you have read this series of articles published before, I believe you are all familiar with date and time.

So for the specific question "How to write a PHP function to get the start and end dates of the week (by week number) in a specific year", can anyone quickly come up with an implementation method?

The following is an implementation method I provided, you can refer to it:

The PHP code is as follows:

<?php

function Start_End_Date_of_a_week($week, $year)
{
    $time = strtotime("1 January $year", time());
    $day = date(&#39;w&#39;, $time);
    $time += ((7*$week)+1-$day)*24*3600;
    $dates[0] = date(&#39;Y-n-j&#39;, $time);
    $time += 6*24*3600;
    $dates[1] = date(&#39;Y-n-j&#39;, $time);
    return $dates;
}

$result = Start_End_Date_of_a_week(12,2020);
echo &#39;本周开始日期: &#39;. $result[0]."<br>";
echo &#39;本周结束日期: &#39;. $result[1];
Copy after login

The output result is :

PHP Date and Time Application 9: Get the start and end dates of a certain week in a certain year

The parameters we give here are the year (2020), the number of weeks (12 weeks), and the final calculated date results are 2020-3- 23. 2020-3-29.

strtotime Function: Parses any English text date or time description into a Unix timestamp;

time Function: Returns from the Unix epoch ( The number of seconds since the current time (January 1 1970 00:00:00 GMT);

date function: formats the local date and time and returns the formatted date string;

In this example "date('w', $time);", the parameter w represents the number of the day of the week (0 represents Sunday [Sunday], 6 represents Saturday [Saturday]).

The parameter Y in "date('Y-n-j', $time);" represents the four-digit number of the year; n represents the number of the month without leading zeros (1 to 12 ); j represents the day of the month without leading zeros (1 to 31).

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of PHP Date and Time Application 9: Get the start and end dates of a certain week in a certain year. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template