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?
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.
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.
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.
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With