The following is a code for getting the start and end dates of a 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(' N ', $startday))! = ' 1 ') {
- $startday = Strtotime ("next Monday", strtotime($year _start)); //Get 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 ("last Sunday", 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 () Gets the number of weeks of the first and last day of the year by passing in the parameter $year year, calculates the date of the first week, and gets the date of the first and last day of each week by looping. The last return is an array.
To 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, the code is as follows:
- $weeks = Get_week (+);
- echo' 18th week start date: '. $weeks [18][0]. '' ;
- echo' 18th Week End Date: '. $weeks [18][1];
The result of the final output:
- 18th Week Start Date: 2011.05.02
- 18th Week End Date: 2011.05.08
Http://www.helloweba.com/view-blog-125.html
http://www.bkjia.com/PHPjc/445825.html www.bkjia.com true http://www.bkjia.com/PHPjc/445825.html techarticle The following is a code for getting the start and end dates of a week in a year through PHP. function Get_week ($year) {$year _start = $year. -01-01; $year _end = $year. -12-...