1

I am trying to calculate the time difference between two timestamps. After looking around here for a while I found this solution:

$start = strtotime(10:00:00);
$end = strtotime(10:30:00);
$diff = ($end - $start);

echo 'Time 1: '.date('H:i:s', $start).'<br>';
echo 'Time 2: '.date('H:i:s', $end).'<br>';
echo 'Diff: '.date('H:i:s', $diff);

It seems to work, the time 1 and time 2 show the correct times, but the difference shows 01:30:00, not 00:30:00 as it should. I have tried setting the correct timezone but it didnt help. Any ideas?

Markus Finell
  • 328
  • 1
  • 3
  • 14

2 Answers2

1

Refer Dates diff in PHP

$date1 = "2013-08-11";
$date2 = "2012-07-12";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Or

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 

//or simply
//echo "difference " . $interval->days . " days ";

Note

It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
softvar
  • 17,917
  • 12
  • 55
  • 76
  • I want the difference in time, not between dates. And I havent been able to figure out much from searching on php.net.. – Markus Finell Apr 14 '14 at 21:30
  • @kylmark The principle still applies, especially if you take the time to read a bit of the PHP documentation on these functions, especially DateTime and DateTimeInterval objects – Mark Baker Apr 14 '14 at 22:59
  • Try this @kylmark :: `http://sandbox.onlinephpfunctions.com/code/b643b9986bcce6c1b0bd25defdc80624df8b13a3` – softvar Apr 14 '14 at 23:06
  • I have commented the `var_dump($interval);` line. Uncomment it and you could see a lot of data you can use. The second method i gave in answer was the one you want, just change date to time and the corresponding variables in the `echo` part – softvar Apr 14 '14 at 23:08
  • Mark it as an answer if it solves your problem, so that it would get closed. – softvar Apr 14 '14 at 23:13
  • @MarkBaker I cant find anything called DateTimeInterval in php :S there's dateinterval? I just cant see why my example doesnt work, shouldnt it? – Markus Finell Apr 14 '14 at 23:18
  • The second solutions above seems to work. I get it to say 8:0 for example, as in 8 hours and zero minutes, how do I get it to say 08:00? – Markus Finell Apr 14 '14 at 23:37
0

When you need to format seconds to HH:MM:SS don't use date() function, since it formats timestamp based on current timezone. If you would use gmadate() for formating time, your problem will be solved:

echo 'Diff: ' . gmdate('H:i:s', $diff);

Convert seconds to HH:MM:SS format: https://stackoverflow.com/a/20870843/67332

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107