防止在特定时间之间进行注册


Prevent registration between certain hours

我正在尝试为我的论坛软件(vBulletin 3.x,它通过钩子系统支持PHP例程)找出一个小例程。我从之前做过的一些研究中获得了这段代码,但条件没有按我的预期运行 - 只有当开始时间设置为 0000 或更晚时,它才是"true"。

$regtime = gmdate('Hi');
$pnr_b1 = "2300";
$pnr_b2 = "0600";
if ($regtime > $pnr_b1 && $regtime < $pnr_b2) {
 // prevent registration code
}

谁能帮忙?作为参考,我正在运行PHP版本5.3.29,以防影响您的建议。

除了比较字符串,您还可以使用DateTime对象,以便可以轻松比较时间:

$regtime = new DateTime('23:15');
$from_time = new DateTime('23:00');
$to_time = new DateTime('23:30');
if($regtime >= $from_time && $regtime <= $to_time) {
    echo 'okay, process this';
} else {
    echo 'not allowed';
}

有效:

<?php
$regtime = gmdate('Gi');
$pnr_b1 = 2300;
$pnr_b2 = 600;
if ($regtime > $pnr_b1 or $regtime < $pnr_b2) {
    echo "fail";
}