This example describes how PHP gets the start and end dates for each week of the year. Share to everyone for your reference. The specific analysis is as follows:
Recent projects need to do a weekly reporting function, you need to know the start date and end date of the specified number of weeks in order to handle other business. Here's a section of code that uses PHP to get the start and end dates of each week of the year to share with you.
Here's a section of code to get the start and end dates of each week in a year through PHP.
function Get_week ($year) {
$year _start = $year. " -01-01";
$year _end = $year. " -12-31";
$startday = Strtotime ($year _start);
if (Intval (date (' N ', $startday))!= ' 1 ') {
$startday =strtotime ("Nextmonday", Strtotime ($year _start));
Gets the date of the first week of the year
}
$year _mondy = Date ("y-m-d", $startday);//Get the date of the first week of the year
$endday = Strtotime ($year _end);
if (intval (' W ', $endday)) = = ' 7 ') {
$endday =strtotime ("Lastsunday", Strtotime ($year _end));
}
$num = intval (Date (' W ', $endday));
for ($i = 1; $i <= $num; $i + +) {
$j = $i-1;
$start _date = Date ("y-m-d", Strtotime ("$year _mondy $j Week"));
$end _day = Date ("y-m-d", Strtotime ("$start _date +6 Day"));
$week _array[$i] = Array (
str_replace ("-", ".", $start _date), Str_replace ("-", ".", $end _day));
}
return $week _array;
}
The function Get_week () $year the year by passing in the parameter, gets the number of weeks of the first and last day of the year, calculates the first week's date, and iterates through the first and last days of each week. The last return is an array.
Get the start and end dates for a specified number of weeks, such as the start and end dates for the 18th week of 2011, as follows:
$weeks = Get_week ();
Echo ' 18th week start date: '. $weeks [18][0].
Echo ' 18th week End Date: '. $weeks [18][1];
Final output results:
1. Week 18th start Date: 2011.05.02
2.18th Week End Date: 2011.05.08
I hope this article will help you with your PHP program design.