Mar, 9
2009

PHP: Check if current time is between 2 values

Here’s a quick and easy script to check if the current time (by timezone) is between 2 predefined values:

function isbetween($s, $e) {
    putenv("TZ=US/Eastern"); //Sets the current timezone
    $ret = False; //Defaults to false
    $n = date("G:i"); //Get current time in 24 hour format
    $n = explode(':',$n); //Separate hours and minutes
    $s = explode(':',$s);
    $e = explode(':',$e);
    $nt = $n[0] * 60 + $n[1];  //Convert time to total minutes
    $st = $s[0] * 60 + $s[1];
    $et = $e[0] * 60 + $e[1];
    if($nt > $st && $nt < $et) //Check time
    $ret = True;
    return $ret;
}
if(isbetween('9:30', '16:00'))
{
echo "It is later than 9:30AM and earlier than 4PM EST";
}

Leave a Reply

*