• PHP 日期分段


    /**
     * 获取日期之间的差数
     */
    
    function prDates($start,$end,$type='+1 day'){ // 两个日期之间的所有日期
        $dt_start = strtotime($start);
        $dt_end = strtotime($end);
        $arr = [];
        while ($dt_start<=$dt_end){
            $arr[]= date('Y-m-d',$dt_start);
            $dt_start = strtotime($type,$dt_start);
        }
        return $arr;
    }
    
    /**
     * 获得开始日期和结束日期 周时段
     * @param $start_date
     * @param $end_date
     * @return array
     */
    function prWeek($start_date,$end_date)
    {
        if(!$start_date || !$end_date) {
            return [];
        }
    
        $temp = $start_date;
        $week_arr = [];
        $end_time = strtotime($end_date);
    
        do{
    
            $start_time = strtotime($start_date);
            $next_day = date("Y-m-d",strtotime("+1 day",$start_time));
    
            if (date('w',$start_time) === '0') {
                $week_arr[] = $temp . '~' . $start_date;
                $temp = $next_day;
            }
    
            $start_date = $next_day;
    
        }while( $start_time < $end_time);
    
        if (date('w',$end_time) !== '0') {
            $week_arr[] = $temp . '~' . $end_date;
        }
    
        return $week_arr;
    }
    
    /**
     *  获取月时段
     *
     * @param string $start_date
     * @param string $end_date
     * @return array
     */
    function prMonths($start_date='',$end_date='')
    {
        if(!$start_date || !$end_date) {
            return [];
        }
    
        $temp_date = $start_date;
        $month_arr = [];
        $end_time = strtotime($end_date);
    
        do {
    
            $start_date = date('Y-m-d', strtotime("$start_date +1 month -1 day"));
            $start_time = strtotime($start_date);
            if($end_time<$start_time) {
                $month_arr[] = $temp_date . '~' . $end_date;
                break;
            } else {
                $month_arr[] = $temp_date . '~' . $start_date;
            }
            $temp_date = $start_date = date('Y-m-d', strtotime("+1 day",$start_time));
    
        }while($start_time < $end_time);
    
        return $month_arr;
    }
    
    /**
     *  获取季度时段
     *
     * @param string $start_date
     * @param string $end_date
     * @return array
     */
    function prQuarter($start_date='',$end_date='')
    {
        $end_time = strtotime($end_date);
        $temp_date = $start_date;
        $quarter_arr = [];
        while(($start_time = strtotime($start_date))<$end_time) {
    
            $season = ceil((date('n',$start_time))/3);
            $start_quarter = date('Y-m-d', mktime(0, 0, 0,$season*3-3+1,1,date('Y',$start_time)));
    
            $end_quarter = date('Y-m-d', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y",$start_time))),date('Y',$start_time)));
            $end_quarter_time = strtotime($end_quarter);
            $start_date = date('Y-m-d', strtotime("+1 day",$end_quarter_time));
    
    
            if($end_quarter_time>$end_time) {
                $quarter_arr[] = $start_quarter.'~'.$end_date;
                break;
            } elseif(strtotime($start_quarter)<strtotime($temp_date)){
                $quarter_arr[] = $temp_date.'~'.$end_quarter;
            } else {
                $quarter_arr[] = $start_quarter.'~'.$end_quarter;
            }
        }
    
        return $quarter_arr;
    }
    
    /**
     *  获取年时段
     *
     * @param string $start_date
     * @param string $end_date
     * @return array
     */
    function prYears($start_date='',$end_date='')
    {
        if(!$start_date || !$end_date) {
            return [];
        }
        $temp_date = $start_date;
        $year_arr = [];
        $end_time = strtotime($end_date);
        while(($start_time=strtotime($start_date))<$end_time) {
    
            $start_date = date('Y-12-01',$start_time);
            $start_date = date('Y-m-d', strtotime("$start_date +1 month -1 day"));
            if($end_time<$start_time) {
                $year_arr[] = $temp_date . '~' . $end_date;
                break;
            } else {
                $year_arr[] = $temp_date . '~' . $start_date;
            }
            $temp_date = $start_date = date('Y-m-d', strtotime("$start_date +1 day"));
        }
        return $year_arr;
    }
  • 相关阅读:
    将博客搬至CSDN
    Fish 下报错 Unsupported use of '||'. In fish, please use 'COMMAND; or COMMAND'.
    使用VMWare虚拟机打开MFC报错:不支持16位系统
    CodeForce Div 2 C. Masha and two friends
    POJ1426 Find The Multiple
    Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion
    CodeForce Educational round Div2 C
    Tenka 1 Computer Contest C-Align
    CodeForce edu round 53 Div 2. D:Berland Fair
    CodeForce 517 Div 2. C Cram Time
  • 原文地址:https://www.cnblogs.com/LXJ416/p/6878802.html
Copyright © 2020-2023  润新知