HP calculates the number of weeks of a year, and returns the start time and end time of a week (optional, return the timestamp or date)

Source: Internet
Author: User
HP calculates the number of weeks of a year, and returns the start time and end time of a week (optional, return the timestamp or date)

  1. Function getWeekStartAndEnd ($ year, $ week = 1 ){
  2. Header ("Content-type: text/html; charset = utf-8 ");
  3. Date_default_timezone_set ("Asia/Shanghai ");
  4. $ Year = (int) $ year;
  5. $ Week = (int) $ week;
  6. // Calculate the total number of weeks of the current year based on the given year
  7. $ Date = new DateTime;
  8. $ Date-> setISODate ($ year, 53 );
  9. $ Weeks = max ($ date-> format ("W"), 52 );
  10. // If the specified number of weeks is greater than the total number of weeks or less than or equal to 0
  11. If ($ week> $ weeks | $ week <= 0 ){
  12. Return false;
  13. }
  14. // If the number of weeks is less than 10
  15. If ($ week <10 ){
  16. $ Week = '0'. $ week;
  17. }
  18. // The start and end timestamp of the week
  19. $ Timestamp ['start'] = strtotime ($ year. 'W'. $ week );
  20. $ Timestamp ['end'] = strtotime ('+ 1 week-1 Day', $ timestamp ['start']);
  21. // Start and end dates of the week
  22. $ Timeymd ['start'] = date ("Y-m-d", $ timestamp ['start']);
  23. $ Timeymd ['end'] = date ("Y-m-d", $ timestamp ['end']);
  24. // Returns the start timestamp.
  25. Return $ timestamp;
  26. // Return Date format
  27. // Return $ timeymd;
  28. }


(Optional) number of instances in a week

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.