I have a problem in comparing and getting the difference between two times in PHP. I tried some answers in the other questions too but none of them answers my problem. Here is the date times I have to compare.
$time_must = strtotime("06:45:00 AM");
$time_in = strtotime("06:48:00 AM");
In that case, the $time_in
should greater than the $time_must
but the result is the $time_must
is still ahead.
This is what I have tried to compare two times:
if (strtotime($row['time_in']) > $time_must) {
echo "You are late.";
}
Am I doing it wrong? Your help and advice will be appreciated.
2
Answers
I don’t know what’s in your
$row['time_in']
, maybe check that, but what you have here works correctly:I believe the issue could be coming from the way you’re retrieving
$row['time_in']
and converting it usingstrtotime
, as it’s not clear to me what format$row['time_in']
is in. It needs to be in a format thatstrtotime
understands. Otherwise, the comparison could fail.So if you’re retrieving
$row['time_in']
from a database or another source, ensure that it is in a correct time format thatstrtotime
can convert correctly. And if$row['time_in']
is already a string representing a time, then I do not think you need to usestrtotime
again on it.