php 時間戳 總結 今日,昨日,上週,本週,最近三個月,上季,本季,去年,最近七天,今年,最近三十天

木子小僧發表於2016-01-27
if($time=="今日"){
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            $begin = Date("Y-m-d",$beginToday);
            $end = Date("Y-m-d",$endToday);
        }else if($time=="昨日"){
            $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            $begin = Date("Y-m-d",$beginYesterday);
            $end = Date("Y-m-d",$endYesterday);
        }else if($time=="上週"){
            $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
            $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
            $begin = Date("Y-m-d",$beginLastweek);
            $end = Date("Y-m-d",$endLastweek);
        }else if($time=="本週"){
            //判斷當天是星期幾,0表星期天,1表星期一,6表星期六
            $w_day=date("w",time());
            //php處理當前星期時間點上,根據當天是否為星期一區別對待
            if($w_day=='1'){
                $cflag = '+0';
                $lflag = '-1';
            }else {
                $cflag = '-1';
                $lflag = '-2';
            }
            //本週一零點的時間戳
            $frist = strtotime(date('Y-m-d',strtotime("$cflag week Monday", time())));
            $begin = Date("Y-m-d",$frist); 
            $end = Date("Y-m-d",time()); 
        }else if($time=="本月"){
            $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
            $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $begin = Date("Y-m-d",$beginThismonth);
            $end = Date("Y-m-d",$endThismonth);
        }else if($time=="上月"){
            $beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));
            $endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y'));
            $begin = Date("Y-m-d",$beginLastmonth);
            $end = Date("Y-m-d",$endLastmonth);
        }else if($time=="最近三個月"){
            $beginThreemonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $endThreemonth=mktime(23,59,59,date('m')-3,date('t'),date('Y'));
            $begin = Date("Y-m-d",$beginThreemonth);
            $end = Date("Y-m-d",$endThreemonth);
        }else if($time=="本季"){
            $begin = date('Y-m-d', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
            $end = date('Y-m-d', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')));
        }else if($time=="上季"){
            $season = ceil((date('n'))/3);//當月是第幾季度
            $begin = date('Y-m-d', mktime(0, 0, 0,$season*3-3-3+1,1,date('Y')));
            $end = date('Y-m-d', mktime(23,59,59,$season*3-3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y')));
        }else if($time=="今年"){
            $begin = date('Y',time()). '-' ."01-01";
            $end = date('Y',time()). '-' ."12-31";
        }else if($time=="去年"){
            $d=strtotime('-1 years');
            $begin = date('Y',$d)."-01-01";
            $end = date('Y',$d)."-12-31";
        }else if($time=="最近七天"){
            $begin = date('Y-m-d', strtotime('-7 days'));
            $end = date('Y-m-d',time());
        }else if($time=="最近三十天"){
            $begin = date('Y-m-d', strtotime('-30 days'));
            $end = date('Y-m-d',time());
        }

 

相關文章