PHP获取一年有多少周和每周开始和结束日期
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//周日
)
----------------------------------------------------------------------------------------------------------------------------
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周
但上面这个获取一年每周的日期遇到头一年最后几天和今年的开始几天是第一周时就不好用了(如2019年第一周是2018-12-31至2019-01-06)而我这个上面的第一周是从2019-01-07至2019-01-13
---------------------------------------------------------------------------------------------------------------------------------------------
如果遇到上面的情况要求周数严谨,可以用下面的:
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 }
得到结果:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
版权声明:本文为博主原创文章,转载请附上博文链接!