1 /*PHP獲取當前日期是第幾周和本週開始日期和本週結束日期*/ 2 //$now = '2018-11-13';週二 3 public function getNowTimeInfo($now) 4 { 5 $str = array(); 6 //$first =1 表示每週星期一爲開始日期 0表示每週日爲開始日期 7 $str['year'] = date('Y', strtotime($now)); 8 $first = 1; 9 //當日在全年中的第幾周 10 $str['week'] = date('W', strtotime($now)); 11 //獲取當前周的第幾天 週日是 0 週一到週六是 1 - 6 12 $w = date('w', strtotime($now)); 13 //獲取本週開始日期,若是$w是0,則表示週日,減去 6 天 14 $week_start = date('Y-m-d', strtotime("$now -" . ($w ? $w - $first : 6) . ' days')); 15 $str['week_start'] = $week_start; 16 //本週結束日期 17 $week_end = date('Y-m-d', strtotime("$week_start +6 days")); 18 $str['week_end'] = $week_end; 19 return $str;
返回值:數組
Array
(
[year] => 2018
[week] => 46
[week_start] => 2018-11-12//週一
[week_end] => 2018-11-18//週日
)this
----------------------------------------------------------------------------------------------------------------------------spa
1 /** 2 * 獲取一年每週的開始日期和結束日期 3 * @param $year 4 * @return mixed 5 */ 6 public static function get_week($year) 7 { 8 $year_start = $year . "-01-01"; 9 $year_end = $year . "-12-31"; 10 $startday = strtotime($year_start); 11 if (intval(date('N', $startday)) != '1') { 12 $startday = strtotime("next monday", strtotime($year_start)); //獲取年第一週的日期 13 } 14 $year_mondy = date("Y-m-d", $startday); //獲取年第一週的日期 15 16 $endday = strtotime($year_end); 17 if (intval(date('W', $endday)) == '7') { 18 $endday = strtotime("last sunday", strtotime($year_end)); 19 } 20 21 //$num = intval(date('W', $endday));能夠獲取當年之前的年份有多少周,若是是當年還沒到12-31號$num爲1 22 $num = 52;//一年約52周 23 for ($i = 1; $i <= $num; $i++) { 24 $j = $i - 1; 25 $start_date = date("Y-m-d", strtotime("$year_mondy $j week ")); 26 27 $end_day = date("Y-m-d", strtotime("$start_date +6 day")); 28 29 $week_array[$i] = array( 30 $start_date, $end_day); 31 } 32 return $week_array; 33 }
返回值:共52周.net
但上面這個獲取一年每週的日期遇到頭一年最後幾天和今年的開始幾天是第一週時就很差用了(如2019年第一週是2018-12-31至2019-01-06)而我這個上面的第一週是從2019-01-07至2019-01-13code
---------------------------------------------------------------------------------------------------------------------------------------------orm
若是遇到上面的狀況要求週數嚴謹,能夠用下面的:blog
1 function get_week_all($year) 2 { 3 $whole_week = $this->get_big_week($year);//獲取最大週數 4 $str = ''; 5 for ($i = 1; $i <= $whole_week; $i++) { 6 $str .= $i . ','; 7 } 8 $str_arr = explode(',', rtrim($str, ','));//轉成數組 9 $week_list = array(); 10 foreach ($str_arr as $key => $value) { 11 $week_list[$key]['num'] = $value; 12 $week = $this->get_year_month_week($year, $value);//一週的日期 13 $week_list[$key]['detail'] = "第" . $value . "周(" . $week[0] . "至" . $week[6] . ")"; 14 } 15 return $week_list; 16 }
1 function get_big_week($year_now) 2 { 3 $big_week=$this->get_date("W",NULL,$year_now.'-12-31'); 4 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-30');} 5 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-29');} 6 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-28');} 7 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-27');} 8 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-26');} 9 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-25');} 10 if($big_week==1){$big_week=$this->get_date("W",NULL,$year_now.'-12-24');} 11 return $big_week; 12 }
1 function get_year_month_week($year, $value) 2 { 3 //指定年和周的日期數組 4 $time=new \DateTime(); 5 $time->setISODate($year_now,$week_now,1); 6 $result[]=$time->format('Y-m-d'); 7 $time->setISODate($year_now,$week_now,2); 8 $result[]=$time->format('Y-m-d'); 9 $time->setISODate($year_now,$week_now,3); 10 $result[]=$time->format('Y-m-d'); 11 $time->setISODate($year_now,$week_now,4); 12 $result[]=$time->format('Y-m-d'); 13 $time->setISODate($year_now,$week_now,5); 14 $result[]=$time->format('Y-m-d'); 15 $time->setISODate($year_now,$week_now,6); 16 $result[]=$time->format('Y-m-d'); 17 $time->setISODate($year_now,$week_now,7); 18 $result[]=$time->format('Y-m-d'); 19 return $result; 20 }
獲得結果:get
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------io
1 //PHP計算兩個日期的差值 2 $sdate = new DateTime("1949-10-01"); 3 $edate = new DateTime(); 4 $interval = $sdate->diff($edate); 5 echo "差值 : " . $interval->y . " 年, " . $interval->m." 月, ".$interval->d." 天 ";
---------------------
做者:鍋巴胸
來源:CSDN
原文:https://blog.csdn.net/sinat_15955423/article/details/84033680
版權聲明:本文爲博主原創文章,轉載請附上博文連接!ast