Home  >  Article  >  Backend Development  >  An example of how to get the start date and end date of the week in a certain year using PHP

An example of how to get the start date and end date of the week in a certain year using PHP

jacklove
jackloveOriginal
2018-07-02 18:02:431773browse

The editor below will share with you an example of using PHP to obtain the start date and end date of the first week of a certain year. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

The example is as follows:

/** 
 * 获取某年第几周的开始日期和结束日期 
 * @param int $year 
 * @param int $week 第几周; 
 */ 
 public function weekday($year,$week=1){ 
  $year_start = mktime(0,0,0,1,1,$year); 
  $year_end = mktime(0,0,0,12,31,$year); 
  // 判断第一天是否为第一周的开始 
  if (intval(date('W',$year_start))===1){ 
   $start = $year_start;//把第一天做为第一周的开始 
  }else{ 
   $week++; 
   $start = strtotime('+1 monday',$year_start);//把第一个周一作为开始 
  } 
  // 第几周的开始时间 
  if ($week===1){ 
   $weekday['start'] = $start; 
  }else{ 
   $weekday['start'] = strtotime('+'.($week-0).' monday',$start); 
  } 
  // 第几周的结束时间 
  $weekday['end'] = strtotime('+1 sunday',$weekday['start']); 
  if (date('Y',$weekday['end'])!=$year){ 
   $weekday['end'] = $year_end; 
  } 
  return $weekday; 
 } 
 /** 
 * 计算一年有多少周,每周从星期一开始, 
 * 如果最后一天在周四后(包括周四)算完整的一周,否则不计入当年的最后一周 
 * 如果第一天在周四前(包括周四)算完整的一周,否则不计入当年的第一周 
 * @param int $year 
 * return int 
 */ 
 public function week($year){ 
  $year_start = mktime(0,0,0,1,1,$year); 
  $year_end = mktime(0,0,0,12,31,$year); 
  if (intval(date('W',$year_end))===1){ 
   return date('W',strtotime('last week',$year_end)); 
  }else{ 
   return date('W',$year_end); 
  } 
 }

The above example of PHP getting the start date and end date of the week of a certain year is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

Articles you may be interested in:

PHP interface multiple inheritance and tarits methods to achieve multiple inheritance effects_php skills

Detailed explanation of the cache() usage of thinkPHP5 framework database coherent operation

Explanation of the method of implementing paging custom style in thinkPHP3.2

The above is the detailed content of An example of how to get the start date and end date of the week in a certain year using PHP. For more information, please follow other related articles on the PHP Chinese website!

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