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
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('w', $time); $time += ((7*$week)+1-$day)*24*3600; $dates[0] = date('Y-n-j', $time); $time += 6*24*3600; $dates[1] = date('Y-n-j', $time); return $dates; } $result = Start_End_Date_of_a_week(12,2020); echo '本周开始日期: '. $result[0]."<br>"; echo '本周结束日期: '. $result[1];
The output result is :
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!