Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two 24 hour time

Tags:

php

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?

like image 371
Ramprasad Avatar asked Jan 02 '12 04:01

Ramprasad


People also ask

How does 24 hour time compare?

Converting from 24 hour to 12 hour clockFrom 1:00 to 11:59, simply add AM to the time: 2:25 = 2:25 AM. 9:30 = 9:30 AM.

How do you compare two time strings?

Use string comparison to compare two time strings, that are formatted as hh:mm:ss , e.g. if (time1 > time2) {} . The the time strings are formatted as hh:mm:ss and are based on a 24 hour clock, the default behavior for string comparison is sufficient.

How to compare time strings in c#?

var value = "00:00:15:185"; if (DateTime. ParseExact(value, "HH:mm:ss:fff", CultureInfo. InvariantCulture) > DateTime. ParseExact("00:00:15:000", "HH:mm:ss:fff", CultureInfo.


2 Answers

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
}
like image 84
rsaxvc Avatar answered Oct 05 '22 13:10

rsaxvc


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.

like image 33
Niet the Dark Absol Avatar answered Oct 05 '22 12:10

Niet the Dark Absol