time difference in hours php code example

Example 1: get hours difference between two dates in php

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

Example 2: difference of two dates in seconds php

$timeFirst  = strtotime('2011-05-12 18:20:20');
$timeSecond = strtotime('2011-05-13 18:20:20');
$differenceInSeconds = $timeSecond - $timeFirst;

Example 3: calculate time difference php

$time1 = new DateTime('09:00:59');
$time2 = new DateTime('09:01:00');
$interval = $time1->diff($time2);
echo $interval->format('%s second(s)');

Result: 1 second(s)

Tags:

Php Example