phptimedate-range

Check if time expression fall inside of time range that crosses over midnight


I have following code I am trying to compare two get in order to get into if statement but I something wrong with is code.

the following code should run if the time is above 23:29 and less then 08:10...

$gettime = "04:39"; // getting this from database

$startdate = strtotime("23:29");
$startdate1 = date("H:i", $startdate);

$enddate = strtotime("08:10");
$enddate1 = date("H:i", $enddate);

if ($gettime >= strtotime($startdate1) && $gettime <= strtotime($enddate1)) {
    echo "ok working";
}

Solution

  • Make sure your comaring the right types of data, time stamps with time stamps and not w/ strings etc...

    $gettime= strtotime("22:00"); // getting this from database
    
    $startdate = strtotime("21:00");
    //$startdate1 = date("H:i", $startdate);
    
    $enddate = strtotime("23:00");
    //$enddate1 = date("H:i", $enddate);
    
    
    //this condition i need to run    
    
    if($gettime >= $startdate && $gettime <= $enddate)
    {
    echo"ok working";
    }