Home >Backend Development >PHP Tutorial >PHP gets the start date and end date of the week of a certain year

PHP gets the start date and end date of the week of a certain year

小云云
小云云Original
2018-01-11 10:43:541943browse

This article mainly shares an example of how to obtain the start date and end date of the week of a certain year in PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

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); 
  } 
 }

Related recommendations:

php Get the start date and end date of this week

PHP Get the start date and end date of the month where the specified date is located_PHP tutorial

How to use PHP to get the start date and end date of the month where the specified date is located

The above is the detailed content of PHP gets the start date and end date of the week of a certain year. 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