Search code examples
phpdatetimecomparison

PHP: How to compare a time string with date('H:i')?


I have time saved in database like 7:30pm as a varchar field. I want to check if this time is greater than time right now or not.

I converted the DB time string into '19:30' and now I want to do something like this:

$my_time = '19:30';

if($my_time  > date('H:i'))
{
    do something ...
}

The problem is the above will return always true if $my_time is non-empty string.

doing strtotime($my_time) is not helping either.

strtotime('H:i',$my_time) makes it 00:00 .

doing (int)date('H:i') will give 1700 when the actual time is 17:09, so removing colon and then comparing will not work too ....

Changing database time data is out of question in this context.

plz help. Correct me if I stated some facts wrong.


Solution

  • You can use this:

    $myTime = '19:30';
    if (date('H:i') == date('H:i', strtotime($myTime))) {
        // do something
    }