Search code examples
phptimestampunix-timestamp

How to compare two time in PHP accurately?


time() function in PHP used to get current time.But there's a problem! Suppose we get the current time 12:05 AM (EST) using this function, then if i compare this time with another time(For example,10:50 AM),obviously PHP will consider that the timestamp of 10:50 AM is greater than 12:05 AM)..but actually it's wrong!12:05 is basically 00:05 AM...!!! I don't know how to compare times like this accurately,any help would be appreciated.

I'm using this block of code to compare those two times...

$current_time = time(); //suppose current time is 12:15 AM(returned by time() func)
$time1 = '1:30 AM';
$draw_time = strtotime($time1);
if($current_time < $draw_time1) {
   echo 'Perfectos!';
} 

Solution

  •    $current_time = time();
        $time1 = date('d.m.Y') . ' 11:42 AM';
        $draw_time = strtotime($time1);
        echo ($draw_time) . " " . $current_time . "\r\n";
        if ($current_time < $draw_time) {
            echo 'True';
        } else {
            echo 'False';
        }
    

    You can try this way Your code is incorrect , bacause you get first variable for only hour and second variable for whole time