PHP Gets an instance of the start and end dates of the week of a year

  • 2021-09-04 23:44:03
  • OfStack

The example is as follows:


/** 
 *  Get the start and end dates of the week of a year  
 * @param int $year 
 * @param int $week  What 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); 
  //  Judgement number 1 Is the day the first 1 The beginning of the week  
  if (intval(date('W',$year_start))===1){ 
   $start = $year_start;// Put the first 1 Heaven is the first 1 The beginning of the week  
  }else{ 
   $week++; 
   $start = strtotime('+1 monday',$year_start);// Put the first 1 Week 1 As a start  
  } 
  //  What week is the start time  
  if ($week===1){ 
   $weekday['start'] = $start; 
  }else{ 
   $weekday['start'] = strtotime('+'.($week-0).' monday',$start); 
  } 
  //  What is the end time of the week  
  $weekday['end'] = strtotime('+1 sunday',$weekday['start']); 
  if (date('Y',$weekday['end'])!=$year){ 
   $weekday['end'] = $year_end; 
  } 
  return $weekday; 
 } 
 /** 
 *  Calculation 1 How many weeks are there in the year, from week to week 1 At first,  
 *  If in the end 1 The day is in the week 4 After (including weeks 4 ) Complete 1 Week, otherwise it will not be included in the last of the year 1 Week  
 *  If the 1 The day is in the week 4 Before (including weeks 4 ) Complete 1 Week, otherwise it will not be included in the first of the year 1 Week  
 * @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 articles: