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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37131747

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值