21

I have two 24-hour time values and want to compare them using PHP.

I have tried the following:

$time="00:05:00"; //5 minutes

if($time1<='00:03:00')
{
 //do some work
}
else
{
 //do something
}

Is this the correct way to compare 2 time values using PHP?

3 Answers 3

46

Use the built-in function strtotime():

$time="00:05:00"; //5 minutes
if(strtotime($time)<=strtotime('00:03:00')) {
 //do some work
} else {
 //do something
}
0
11

Yes, that is correct. You don't need to convert to an integer because 24-hour format is such that the string value is in the exact same order as the numeric.

3
  • Oh really? Such as what, exactly? Jan 2, 2012 at 17:08
  • 5
    "24:00:00" > "00:00:00", is it not? Jan 2, 2012 at 21:57
  • 1
    "24:00:00" means "+1 day" in my books. Not that I'd ever use it, I always have the date value in my comparisons. Jan 2, 2012 at 23:07
4

You could use, php's strtotime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.