Search code examples
phpif-statementtimevbulletin

Prevent registration between certain hours


I'm trying to figure a little routine for my forum software (vBulletin 3.x which supports PHP routines via a hook system). I have this code from some research I did before but the conditional isn't functioning as I intended - it will only be 'true' if the start time is set to 0000 or later.

$regtime = gmdate('Hi');
$pnr_b1 = "2300";
$pnr_b2 = "0600";

if ($regtime > $pnr_b1 && $regtime < $pnr_b2) {
 // prevent registration code
}

Can anyone help? For reference I'm running PHP Version 5.3.29 in case that influences your suggestions.


Solution

  • Instead comparing strings, you could also use DateTime objects so that you could easily compare time:

    $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';
    }