Ramprasad
Ramprasad

Reputation: 8071

How to compare two 24 hour time

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?

Upvotes: 22

Views: 40846

Answers (3)

rsaxvc
rsaxvc

Reputation: 1780

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
}

Upvotes: 47

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

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.

Upvotes: 11

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

You could use, php's strtotime

Upvotes: 4

Related Questions