PHP calculates a time period that has several weeks and a certain time for the first few weeks of the year

Source: Internet
Author: User
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 ' =&gt      ; $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 ')
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.