PHP calculates a time period that has several weeks and a certain time for the first week of the year function
Date_default_timezone_set (' PRC '), function Count_weeks ($startDate, $endDate = ") {//start time $startDate = Date (' y-m-d ', $st Artdate); End Time if (empty ($endDate)) {$endDate = date (' y-m-d '); }else{$endDate = Date (' y-m-d ', $endDate); }//span of days $n = (Strtotime ($endDate)-strtotime ($startDate))/86400; Judging, the span is less than 7 days, may be the same week, or it may be two weeks $endDate = Date ("y-m-d", Strtotime ("$endDate +1 Day"); if ($n <7) {//Check start time at the location of that week $day = Date ("W", Strtotime ($startDate))-1; Monday $week _start = Date ("y-m-d", Strtotime ("$startDate-{$day} day") for the start of the week; Check the start time that week weekend $day = 7-$day; $week _end = Date ("y-m-d", Strtotime ("$startDate +{$day} Day")); Determines whether the weekend time is greater than the end time of the time period, if greater than, that is, the time period in the same week, otherwise the time period spans two weeks if ($week _end>= $endDate) {$weekList [] =array (' s ' => ; $startDate, ' E ' =>date ("y-m-d", Strtotime ("$endDate-1 Day")); }else{$weekList [] =array (' s ' = = $startDate, ' E ' =>date ("y-m-d", Strtotime ("$weeK_end-1 Day ")); $weekList [] =array (' s ' = = $week _end, ' E ' =>date ("y-m-d", Strtotime ("$endDate-1 Day")); }}else{//If the span is greater than or equal to 7 days, it may be just 1 weeks or span 2 weeks or across N weeks, first find out where the start time is in that week and the weekend time of that week $day = Date ("W", Strtotime ($startDate))- 1; $week _start = Date ("y-m-d", Strtotime ("$startDate-{$day} Day")); $day = 7-$day; $week _end = Date ("y-m-d", Strtotime ("$startDate +{$day} Day")); The first week of the start time is written into the array $weekList [] =array (' s ' = = $startDate, ' E ' =>date ("y-m-d", Strtotime ("$week _end-1 Day")); Determines whether the weekend is greater than or equal to the end time, whether greater than (2 weeks) or equal to (1 weeks), the end time is the end time of the time period. if ($week _end >= $endDate) {$weekList [] = Array (' s ' = = $week _end, ' E ' =>date ("y-m-d", Strtotime ("$endDate-1 Day ")); }else{//n Week of the situation with a while loop, and then write to the array while ($week _end <= $endDate) {$start = $week _end; $week _end = Date ("y-m-d", Strtotime ("$week _end +7 Day"); if ($week _end <= $endDate) { $weekList [] = Array (' s ' = = $start, ' E ' =>date ("y-m-d", Strtotime ("$week _end-1 Day")); }else{$weekList [] = Array (' s ' = = $start, ' E ' =>date ("y-m-d", Strtotime ("$endDate-1 Day")); }}}} return $weekList;}
PHP gets some time for this year's week method
Date (' W ')