Home  >  Article  >  Backend Development  >  HP implementation calculates the number of weeks in a year and returns the start time and end time of a week (optional return timestamp or date)

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

WBOY
WBOYOriginal
2016-07-25 08:45:311039browse
  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 in this 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 given week number 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 timestamps of the current 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 current week
  22. $timeymd['start'] = date("Y-m-d",$timestamp['start']);
  23. $timeymd['end'] = date("Y-m-d",$timestamp['end ']);
  24. //Return the starting timestamp
  25. return $timestamp;
  26. //Return the date format
  27. //return $timeymd;
  28. }
Copy code

Optional, one week, how many


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn