php 运行时间限制_php的一个时间段限制

本文介绍了一位开发者自己编写的一个体重记录系统,该系统能够根据时间限制用户提交记录,并检查输入格式。代码中包含了判断用户每日提交次数的逻辑,确保用户在允许的时间段内只能提交一次。此外,系统还会更新客户的提交次数。虽然功能已实现,作者仍在寻求可能的改进方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

9066f4543e0041f4816fdad76051894d.png

慕桂英546537

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

// +---------------------------------------------

// | 体重记录表

// +---------------------------------------------

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。哪位大神还有比较好的方案可以改进下吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值