0

I'd like to calculate the difference in hours between two datetimes elements, but I need to have the exact value of it (like a double variable).

How may I do it?

I have no code samples because I got really stucked in this case, I really need the exact hours of this datetime element and I think date_diff can't give this value in a precise way

MattDAVM
  • 505
  • 3
  • 6
  • 25
  • 3
    Possible duplicate of [Calculate number of hours between 2 dates in PHP](http://stackoverflow.com/questions/3108591/calculate-number-of-hours-between-2-dates-in-php) – 3472948 Mar 14 '17 at 14:38
  • your input example and your output suggest please – plonknimbuzz Mar 14 '17 at 14:44

2 Answers2

1

You seems to look for the getTimestamp method :

<?php

$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2016-02-10 07:00:00');
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2017-03-11 19:00:00');

$diff = diff_hours($date1, $date2);
var_dump($diff);
function diff_hours(DateTime $date1, DateTime $date2) {
    $diff = $date2->getTimestamp() - $date1->getTimestamp();
    return $diff / ( 60 * 60 );
}
Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13
0

You can use below method.

$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 /  60;
PayPal_Kartik
  • 226
  • 2
  • 9