0
点赞
收藏
分享

微信扫一扫

MYSQL - 查询今天、昨天、前天、本周、本月、本季度的数据

westfallon 2022-03-23 阅读 91
mysql

 字段为时间戳

         if($cat_id==1){//今天
             $start = strtotime(date('Y-m-d', time()));  //当前时间 当天0时0分0秒
             $end = $start + 86400; //,获取明天,一天是86400(时间戳)
        }elseif($cat_id==2){//昨天
             $start = strtotime(date('Y-m-d', time()))- 86400; //获取昨天,一天是86400(时间戳)      
             $end = strtotime(date('Y-m-d', time())); 
        }elseif($cat_id==3){//前天
             $start = strtotime(date('Y-m-d', time()))- 86400*2; //获取前天,一天是86400(时间戳) 
             $end = $start+86400 ; 
        }elseif($cat_id==4){//本周
             $start = strtotime(date('Y-m-d', time()).'+1 day -'.date('w', time()).' day');  
             $end = strtotime(date('Y-m-d', time()).'+1 week -'.date('w', time()).' day'.'+1 day') ; 
        }elseif($cat_id==5){//本月
             $start=strtotime(date('Y-m-01 00:00:00'));
             $end = strtotime(date('Y-m-d H:i:s'));
        }else{//本季度
             $month=date('m');
             if($month==1 || $month==2 ||$month==3){
                $start=strtotime(date('Y-01-01 00:00:00'));
                $end=strtotime(date("Y-03-31 23:59:59"));
             }elseif($month==4 || $month==5 ||$month==6){
                $start=strtotime(date('Y-04-01 00:00:00'));
                $end=strtotime(date("Y-06-30 23:59:59"));
             }elseif($month==7 || $month==8 ||$month==9){
                $start=strtotime(date('Y-07-01 00:00:00'));
                $end=strtotime(date("Y-09-30 23:59:59"));
             }else{
                $start=strtotime(date('Y-10-01 00:00:00'));
                $end=strtotime(date("Y-12-31 23:59:59"));
             }
        }

select * from table where ctime between  $start and $end;

举报

相关推荐

0 条评论