获取一年的所有周信息

<?php
    // phpinfo();
class Week
{
    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获取日期的地方都会变成公共方法调用。
    */
    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));
        }
    }

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

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


}

$Week = new Week();
print_r($Week->get_week_all('2022'));

原文链接: 获取一年的所有周信息 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-731.html )

发表评论

0则评论给“获取一年的所有周信息”