对指定时间内的周一到周末进行分组

 const MONDAY = 'Mon';
    const TUESDAY = 'Tue';
    const WEDENSDAY = 'Wed';
    const THURSDAY = 'Thu';
    const FRIDAY = 'Fri';
    const SATURDAY = 'Sat';
    const SUNDAY = 'Sun';

    public function Yeardays($dateStart, $dateend){
        $period = new \DatePeriod(
            new \DateTime($dateStart), new \DateInterval('P1D'), (new \DateTime($dateend))
        );
        $dates = iterator_to_array($period);

        $arrayreturn = array(
            'Mon'=>array(),
            'Tue'=>array(),
            'Wed'=>array(),
            'Thu'=>array(),
            'Fri'=>array(),
            'Sat'=>array(),
            'Sun'=>array(),
        );
        foreach ($dates as $val) {
            $date = $val->format('Y-m-d'); //format date
            $get_name = date('l', strtotime($date)); //get week day
            $day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
            switch ($day_name) {
                case self::MONDAY:
                    $MONDAY[] = $date;
                    $arrayreturn[self::MONDAY] = $MONDAY;
                    break;
                case self::TUESDAY:
                    $TUESDAY[] = $date;
                    $arrayreturn[self::TUESDAY] = $TUESDAY;
                    break;
                case self::WEDENSDAY:
                    $WEDENSDAY[] = $date;
                    $arrayreturn[self::WEDENSDAY] = $WEDENSDAY;
                    break;
                case self::THURSDAY:
                    $THURSDAY[] = $date;
                    $arrayreturn[self::THURSDAY] = $THURSDAY;
                    break;
                case self::FRIDAY:
                    $FRIDAY[] = $date;
                    $arrayreturn[self::FRIDAY] = $FRIDAY;
                    break;
                case self::SATURDAY:
                    $SATURDAY[] = $date;
                    $arrayreturn[self::SATURDAY] = $SATURDAY;
                    break;
                case self::SUNDAY:
                    $SUNDAY[] = $date;
                    $arrayreturn[self::SUNDAY] = $SUNDAY;
                    break;
            }
        }
//        当前时间内周几共有几条记录
//        $array = [
//            '1'=>count($arrayreturn['Mon']),
//            '2'=>count($arrayreturn['Tue']),
//            '3'=>count($arrayreturn['Wed']),
//            '4'=>count($arrayreturn['Thu']),
//            '5'=>count($arrayreturn['Fri']),
//            '6'=>count($arrayreturn['Sat']),
//            '7'=>count($arrayreturn['Sun']),
//        ];
        return $arrayreturn;
    }

调用方式

注:后面的时间最好加一天

$this->Yeardays('2020-07-01','2020-07-31');

 

你可能感兴趣的:(php,php)