What the best way to convert 24 hours time to seconds so it can be used for comparison if statement..
function HourMinuteToDecimal($hour_minute) {
$t = explode(':', $hour_minute);
return $t[0] * 60 + $t[1];
}
echo HourMinuteToDecimal("23:30");
return 1410
If you try to convert midnight time (00:00) to seconds, it will not work. What is the solution to this?
00:00, 00:30, 01:00, 01:30, etc.
if (HourMinuteToDecimal("01:30") > HourMinuteToDecimal("23:30")) { .. }
This will not work.
PHP5.3
You can compare objects of type
time
using the standard<
,>
,==
operators. Usestrtotime
to convert the string to time, then compare.You seem to be asking contradicting things.
HH:MM
into seconds.If you want to allow
HourMinuteToDecimal("01:30") > HourMinuteToDecimal("23:30")
to be true without a date, then, well, any comparison will returntrue
.If you want to do this properly, include the date (
YYYY-MM-DD HH:MM:SS
) and usestrtotime
in PHP to get the number of seconds since the UNIX epoch.Also, your function returns minutes, not seconds.
To convert function:
Write on the fly, no tested :-P
so, you can use strtotime to convert the format date in unix timestamp and comparte using a standar operators (== < > >= <= !=, etc) ex:
etc..