class Week extends Common{ public function get_week_all($year) { $whole_week = $this->get_big_week($year);//獲取最大週數 $str = ''; for ($i = 1; $i <= $whole_week; $i++) { $str .= $i . ','; } $str_arr = explode(',', rtrim($str, ','));//轉成數組 $week_list = array(); foreach ($str_arr as $key => $value) { $week_list[$key]['num'] = $value; $week = $this->get_year_month_week($year, $value);//一週的日期 $week_list[$key]['detail'] = "第" . $value . "周(" . $week[0] . "至" . $week[6] . ")"; } return $week_list; } /* 用法: $this->get_date("Y-m-d","+1 day");當前日期加一天 $this->get_date("Y-m-d","+1 month",$date);指定日期下一月 $this->get_date("Y-m","+1 years",$date);指定日期下一年的年月 $this->get_date("Y-m-t",NULL,$date);指定日期的月底 $this->get_date("m","+1 month",$date);指定日期下一個月的月份(獲取月份時候,日期必填。否則個別日期會有bug存在) 等等,根據需求本身獲取。代碼裏面strtotime獲取日期的地方都會變成公共方法調用。 */ public function get_date($return, $string, $date) { if (empty($return)) { $return = "Y-m-d"; } if (empty($string)) { $string = "now"; } if (empty($date)) { $date = time(); } else { $date = strtotime(date($date)); } if (preg_match("/month/", $string)) { $first_day_of_month = date('Y-m', $date) . '-01'; $str_date = strtotime($first_day_of_month); } if (preg_match("/month/", $string)) { $l_date = date("Y-m-d", strtotime($string, $str_date)); $day = substr(date("Y-m-d", $date), 8, 2); if (substr($l_date, 0, 7) . '-' . date('t', strtotime(substr($l_date, 0, 7))) < substr($l_date, 0, 7) . '-' . $day) { $l_date = substr($l_date, 0, 7) . '-' . date('t', strtotime(substr($l_date, 0, 7))); } else { $l_date = substr($l_date, 0, 7) . '-' . $day; } $l_date = date($return, strtotime($l_date)); return $l_date; } else { return date($return, strtotime($string, $date)); } } public function get_big_week($year_now) { $big_week = $this->get_date("W", NULL, $year_now . '-12-31'); if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-30'); } if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-29'); } if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-28'); } if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-27'); } if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-26'); } if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-25'); } if ($big_week == 1) { $big_week = $this->get_date("W", NULL, $year_now . '-12-24'); } return $big_week; } public function get_year_month_week($year_now, $week_now) { //指定年和周的日期數組 $time = new \DateTime(); $time->setISODate($year_now, $week_now, 1); $result[] = $time->format('Y-m-d'); $time->setISODate($year_now, $week_now, 2); $result[] = $time->format('Y-m-d'); $time->setISODate($year_now, $week_now, 3); $result[] = $time->format('Y-m-d'); $time->setISODate($year_now, $week_now, 4); $result[] = $time->format('Y-m-d'); $time->setISODate($year_now, $week_now, 5); $result[] = $time->format('Y-m-d'); $time->setISODate($year_now, $week_now, 6); $result[] = $time->format('Y-m-d'); $time->setISODate($year_now, $week_now, 7); $result[] = $time->format('Y-m-d'); return $result; } }
調用方法:php
$Week = new Week(); print_r($Week->get_week_all('2019'));