0

I'm trying to get the time difference between the current date and another date in php but i'm getting wrong results. Her's my code so far:

function hoursDifference($date)
{
   return round((time()- strtotime($date))/3600);
}

The input date is: 2016-03-20 03:55:51 but the current time is using the 24 hours format, so the H is 15 instead of 3 and the function returns a correct 12 hour time difference. How can I fix this?

user3077596
  • 55
  • 1
  • 5

3 Answers3

0

For 12 hour Date instead of 24 hour Date use a Lower case 'h'. You should also take a look at DateTime::diff like @fusion3k mentioned.

This link should also help you on which parameters you can use on your $date.

0

Try with this

 function hoursDifference($date)
 {
  return round(strtotime(date("Y-m-d h:i:s")) - strtotime($date))/3600);
 }
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

use the DateTime Object: http://php.net/manual/de/class.datetime.php

echo hoursDifference("2016-03-20 12:13:12");

function hoursDifference($date)
{
    $d = new DateTime($date);
    $now = new DateTime();   
    $iv = $now->diff($d);
    return $iv->h;
}
Harald Ernst
  • 364
  • 4
  • 10