算法 - php的一个时间段限制

查看:128
本文介绍了算法 - php的一个时间段限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

场景:

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

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

问题:

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

谢谢

解决方案

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

// +---------------------------------------------
    // | 体重记录表
    // +---------------------------------------------
    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的一个时间段限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆