skip to Main Content

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


  1. I don’t know what’s in your $row['time_in'], maybe check that, but what you have here works correctly:

    
    php > if (strtotime("06:48:00 AM") > strtotime("06:45:00 AM")) {
    php {     echo "You are late.";
    php { }
    You are late.
    
    Login or Signup to reply.
  2. I believe the issue could be coming from the way you’re retrieving $row['time_in'] and converting it using strtotime, as it’s not clear to me what format $row['time_in'] is in. It needs to be in a format that strtotime 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 that strtotime can convert correctly. And if $row['time_in'] is already a string representing a time, then I do not think you need to use strtotime again on it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search