/**
 * 根据类型获取时间格式
 * @param $time_type : 1年; 2季度;  3月; 4周; 5日;
 * @return time_start  开始时间
 * @return time_end    结束时间
 */
function sys_get_time_around($time_type)
{

    $now_start = 0;
    $now_end=0;
    $last_start= 0;
    $last_end = 0;
    $now_time = time();
    switch ($time_type) {
        case 'year':
            //今年
            $now_start = mktime(0,0,0,1,1,date('Y'));
            $now_end =  mktime(23,59,59,12,31,date('Y'));
            //去年
            $last_start = mktime(0,0,0,1,1,date('Y')-1);
            $last_end =  mktime(23,59,59,12,31,date('Y')-1);
            break;

        case 'quarter':
            $season = ceil((date('n'))/3)-1;//上季度是第几季度
            //本季度
            $now_season = ceil((date('n'))/3);//当月是第几季度
            $now_start = mktime(0, 0, 0,$now_season*3-3+1,1,date('Y'));
            $now_end = mktime(23,59,59,$now_season*3,date('t',mktime(0, 0 , 0,$now_season*3,1,date("Y"))),date('Y'));
            //上一季度
            $last_season = ceil((date('n'))/3)-1;//上季度是第几季度
            $last_start = mktime(0, 0, 0,$last_season*3-3+1,1,date('Y'));
            $last_end = mktime(23,59,59,$last_season*3,date('t',mktime(0, 0 , 0,$last_season*3,1,date("Y"))),date('Y'));
            break;

        case 'month':
            //本月
            $now_start = mktime(0,0,0,date('m'),1,date('Y'));
            $now_end = mktime(23,59,59,date('m'),date('t'),date('Y'));
            //上月
            $last_start = mktime(0,0,0,date('m')-1,1,date('Y'));
            $last_end = mktime(23,59,59,date('m'),0,date('Y'));
            break;

        case 'week':
            //mktime 是从周日开始算,结束时间是周六
            //本周
            // $now_start = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
            // $now_end   = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
            // //上周
            // $last_start = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
            // $last_end   = mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

            //本周
            $now_start =   strtotime(date("Y-m-d 00:00:00",strtotime("this week Monday",$now_time )));
            $now_end   =   strtotime(date("Y-m-d 23:59:59",strtotime("this week Sunday",$now_time )));
            //上周
            $last_start =  strtotime(date("Y-m-d 00:00:00",strtotime("last week Monday",$now_time )));
            $last_end   =  strtotime(date("Y-m-d 23:59:59",strtotime("last week Sunday",$now_time )));
            break;
        case 'day':
            //今日
            $now_start = mktime(0,0,0,date('m'),date('d'),date('Y'));
            $now_end = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            //昨日
            $last_start = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            $last_end = mktime(23,59,59,date('m'),date('d')-1,date('Y'));

            break;
        default:
            # code...
            break;
    }

    $retult = [
        'time_type'=>$time_type,
        'now_start'=>date('Y-m-d H:i:s',$now_start),
        'now_end'=>date('Y-m-d H:i:s',$now_end),
        'last_start'=>date('Y-m-d H:i:s',$last_start),
        'last_end'=>date('Y-m-d H:i:s',$last_end),
    ];


    return $retult;
}

调用

   $a = sys_get_time_around('week');
   print_r($a);exit;

此处一定要注意,本周和上周的时候,mktime是从 周日到周六,避免被坑

Last modification:April 26th, 2020 at 07:58 pm
如果觉得我的文章对你有用,请随意赞赏