PHP判断当前时间是否在时间段内

这是一个PHP函数,用于检查当前时间是否在三个预设的时间段内:早上8点到10点,中午11点到13点,下午16点到18点。通过比较当前时间戳与设定的时间戳,返回相应时间段的编号。适用于签到、抢红包等场景。
摘要由CSDN通过智能技术生成

小编跟大家分享一个当前时间是否在设定的时间段,像特定时间段签到、抢红包等等,都能用的到,上代码。

 function public time_quantum(){
        //当前年月日
        $Date = date('Y-m-d ',time());
        //年月日拼接开始时间
        $Begin1 = strtotime($Date."08:00".":00");
        //年月日拼接截止时间
        $End1 = strtotime($Date."10:00".":00");
        //开始时间
        $Begin2 = strtotime($Date."11:00".":00");
        //截止时间
        $End2 = strtotime($Date."13:00".":00");
        //开始时间
        $Begin3 = strtotime($Date."16:00".":00");
        //截止时间
        $End3 = strtotime($Date."18:00".":00");
        //当前时间
        $curr_time = time();
        //使用逻辑与判断当前时间在哪个时间段
        if($curr_time >= $Begin1 && $curr_time <= $End1){
            return 0;
        }elseif($curr_time >= $tBegin2 && $curr_time <= $End2){
            return 1;
        }elseif($curr_time >= $Begin3 && $curr_time <= $End3){
            return 2;
        }else{
            return 3;
        }
        
    }

有什么不懂的地方,请大家指出来,谢谢ヾ(◍°∇°◍)ノ゙

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值