Calculate the start date and end date of the week based on the Year and week number in php.

Source: Internet
Author: User
If I already know the year and the week of the year (52 weeks in a year), calculate the initial date and end date of the week. The Code is as follows: 1 functionGetWeekDate ($ year, $ week) 2 {3 $ monthsarray (1Jan ., 2Feb ., 3Mar ., 4Apr ., 5May ., 6Jun ., 7Jul ., 8Aug.

If I already know the year and the week of the year (52 weeks in a year), calculate the initial date and end date of the week. The Code is as follows: 1 function GetWeekDate ($ year, $ week) 2 {3 $ months = array ("1" = "Jan. "," 2 "=" Feb. "," 3 "=" Mar. "," 4 "=" Apr. "," 5 "=" May. "," 6 "=" Jun. "," 7 "=" Jul. "," 8 "=" Aug."

If I already know the year and the week of the year (52 weeks in a year), calculate the initial date and end date of the week. The Code is as follows:

  

1 function GetWeekDate ($ year, $ week) 2 {3 $ months = array ("1" => "Jan. "," 2 "=>" Feb. "," 3 "=>" Mar. "," 4 "=>" Apr. "," 5 "=>" May. "," 6 "=>" Jun. "," 7 "=>" Jul. "," 8 "=>" Aug. "," 9 "=>" Sep. "," 10 "=>" Oct. "," 11 "=>" Nov. "," 12 "=>" Dec. "); 4 $ time = strtotime (" 1 January $ year ", time (); 5 $ day = date ('w', $ time ); // calculate the day of week 1st on April 9. 0 indicates Monday, 6 indicates Sunday 6 $ time + = (7 * ($ week-1) + 1-$ day) * 24*3600; // The time returns to the first day of the first week of the year, because January 1, January 1 is not necessarily Monday 7 $ m1 = date ('M', $ time ); 8 $ d1 = date ('D', $ time); 9 $ time + = 6*24*3600; // The interval between the start time and end time of a week is 10 $ m2 = date ('M', $ time); 11 $ d2 = date ('D', $ time ); 12 return $ year. "the week ". $ week. 'th ('. $ months [(int) $ m1]. $ d1 .'-'. $ months [(int) $ m2]. $ d2. ')'; 13}

Test results:

$ Year = 2014;

$ Week = 7; // week 7

Echo GetWeekDate ($ year, $ week); // the result is: 2014 the week 7th (Feb.10-Feb.16)

The initial time (Monday) for the seventh week of January 1, 2014 is 2014.2.10, and the end time (Sunday) is 2014.2.16.

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.