PHP gets the start date and end date of each week of the year _php tutorial

Source: Internet
Author: User
Tags vars
The following is a code for getting the start and end dates of a week in a year through PHP.

 
 
  1. function Get_week ($year) {
  2. $year _start = $year . " -01-01" ;
  3. $year _end = $year . " -12-31" ;
  4. $startday = Strtotime ($year _start);
  5. if (intval(' N ', $startday))! = ' 1 ') {
  6. $startday = Strtotime ("next Monday", strtotime($year _start)); //Get the date of the first week of the year
  7. }
  8. $year _mondy = Date ("y-m-d", $startday); //Get the date of the first week of the year
  9. $endday = Strtotime ($year _end);
  10. if (intval(' W ', $endday ) = = = ' 7 ') {
  11. $endday = Strtotime ("last Sunday", strtotime($year _end));
  12. }
  13. $num = intval (date(' W ', $endday));
  14. for ($i = 1; $i <= $num ; $i ++) {
  15. $j = $i -1;
  16. $start _date = Date ("y-m-d", strtotime("$year _mondy $j Week"));
  17. $end _day = Date ("y-m-d", strtotime("$start _date +6 Day") ;
  18. $week _array [$i] = Array (
  19. Str_replace ("-",
  20. "." ,
  21. $start _date
  22. ), str_replace("-", "." ) , $end _day ));
  23. }
  24. return $week _array;
  25. }

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:

 
  
  
  1. $weeks = Get_week (+);
  2. echo' 18th week start date: '. $weeks [18][0]. '' ;
  3. echo' 18th Week End Date: '. $weeks [18][1];

The result of the final output:

 
  
  
  1. 18th Week Start Date: 2011.05.02
  2. 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-...

  • 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.