php的一个时间段限制

场景:

用户前台有一个添加单日数据的按钮,每天只允许上传2个时间段的数据,一次是当日0点到12点,一次是18点到24点。

不在这两个时间段内均不允许提交,且2个时间段内只允许提交一次。

问题:

 请问该如何设计表,如何判断用户提交的时间段内数据库内是否有对应记录?

谢谢

阅读 6k
3 个回答

自己摸索了一上午,写了一个能运行的方案。

// +---------------------------------------------
    // | 体重记录表
    // +---------------------------------------------
    public function weighe()
    {
        if($this->request->isPost()) {

            $data['weight'] = $this->request->param('weighe', '' , 'trim');
            if(!preg_match("/^\d*$/", $data['weight'])) return $this->error('提交体重格式不正确!');

            $time = time();
            $nowTime = date('H', $time);
            $_time = $nowTime <= 12 ? '早上' : ($nowTime >= 18 ? '晚上' : '');
            $today = Time::today();
            $today[2] = strtotime(date('Y-m-d', $time).' 12:00:00'); // 添加当日12点中午的时间戳到数组中
            if ($nowTime >= 13 && $nowTime < 18) return $this->error('当前时间段不允许提交体重记录');
            // 自动判断当前时间段内是否已经提交记录
            if ($nowTime <= 12) {
                $map['add_at'] = ['between', [$today[0], $today[2]]];
            } elseif (18 <= $nowTime && $nowTime <= 23) {
                $map['add_at'] = ['between', [$today[2], $today[1]]];
            }
            $map['wx_openid'] = $this->userInfo['openid'];
            $row = Db::name('weights')->where($map)->count();
            unset($map);
            if ($row > 0) return $this->error($_time.'已经提交过体重记录了');

            // 执行插入前先判断单日是否已经存在提交
            $map['wx_openid'] = $this->userInfo['openid'];
            $map['add_at'] = ['between', [$today[0], $today[1]]];
            if (1 > Db::name('weights')->where($map)->count()) {
                unset($map);
                $map['customer_weixin_openid'] = $this->userInfo['openid'];
                Db::name('customers')->where($map)->setInc('custmer_add_weight_days'); // 当日第一次提交,更新提交次数到客户表   
            }
            $data['wx_openid'] = $this->userInfo['openid'];
            $data['add_at'] = time();
            if (Db::name('weights')->insert($data) > 0) return $this->success('体重记录提交成功!');
            return $this->error('体重记录提交失败');
        }
        // 提交满指点天数才可发表满意评价
        $map['customer_weixin_openid'] = $this->userInfo['openid'];
        $rows = Db::name('customers')->where($map)->field('custmer_add_weight_days')->find();
        $this->assign('disabled', $rows['custmer_add_weight_days'] > 25 ? false : true);
        unset($map);
        $map['wx_openid'] = $this->userInfo['openid'];
        $rows = Db::name('weights')->where($map)->order('add_at DESC')->field('wx_openid', true)->select();

        $this->assign('list', $this->getData($rows));
        return $this->fetch();
    }

能达到预期的效果,但不知道有没有未知的BUG。
哪位大神还有比较好的方案可以改进下吗?

暂时没有想到其他好的解决办法,提供一个思路抛砖引玉

 <?php
    $morningLimit00 = strtotime(date('00:00:00'));
    $morningLimit12 = strtotime(date('12:00:00'));

    $afternoonLimit18 = strtotime(date('18:00:00'));
    $afternoonLimit24 = strtotime(date('23:59:59'));

    $nowTime = time();
    if($nowTime>=$morningLimit00 and $nowTime <=$afternoonLimit24){
        echo '上午限制时间到了';

        $result = 'SELECT COUNT(*) FORM `table` WHERE `table`.created_at >=:morningLimit00 AND `table`.created_at <= :morningLimit12';

        if($result > 0){
            echo '已经录入过数据';
        }

        //todo 录入数据


    } elseif($nowTime >= $afternoonLimit18 AND $nowTime <= $afternoonLimit24) {
        echo '下午限制时间到了';

        $result = 'SELECT COUNT(*) FORM `table` WHERE `table`.created_at >=:afternoonLimit18 AND `table`.created_at <= :afternoonLimit24';

        if($result > 0){
            echo '已经录入过数据';
        }

        //todo 录入数据


    } else{
        echo '时间没到';
    }

其中$result就是去数据库取有没有限制时间内的数据,我这里写了一段伪代码
记录数据的表里需要有一列created_at记录这条记录的创建时间。

这个问题分为两个方面来处理:

1.表单提交的disable/enable :

前段根据时间做处理按钮,后端增加中间件来根据时间过滤掉请求

2.数据库设计及操作:

除了业务字段外,增加一个hash字段,设置为unique 的约束条件。

数据入库前计算一下当前数据的hash值,一并插入数据库,如果重复提交了,会有报错。

hash函数应该能够根据当前时间输出两个不同的值(且输出范围只有这两个值,对应你的两个时间段),再将当前时间的年-月-日+hash函数输出值合并,作为hash字段一并插入数据库。

这样一天内,如果两个时间段都传过数据了,就无法再次插入新数据了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题