13

Possible Duplicate:
How to calculate the difference between two dates using PHP?

Here i mention two times with its date

2008-12-13 10:42:00

2010-10-20 08:10:00

I want to get total time difference in (h:m:s) format

Community
  • 1
  • 1
meginjoy
  • 151
  • 1
  • 1
  • 7
  • 1
    See this Question: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Tarion May 22 '12 at 06:16
  • 3
    Just check the question @tarion linked, and use the most voted (not the accepted) answer, using Datetime's diff: http://www.php.net/manual/en/datetime.diff.php – Nanne May 22 '12 at 06:19
  • 2
    -1. If you google "php difference between two dates" the exact duplicate that Tarion linked is the first result. – Corbin May 22 '12 at 06:20
  • 1
    THIS IS NOT a duplicate of the question linked to, in fact I found this answer MUCH more helpful...can we remove the duplicate from this question? – Marilee Aug 27 '16 at 00:46

3 Answers3

34

If you are using or able to use PHP 5.3.x or later, you can use its DateTime object functionality:

$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');

$interval = date_diff($date_a,$date_b);

echo $interval->format('%h:%i:%s');

You can play with the format in a variety of ways, and once you have dates in DateTime objects, you can take advantage of a lot of different functionality, for example comparison via normal operators. See the manual for more: http://us3.php.net/manual/en/datetime.diff.php

futureal
  • 3,025
  • 1
  • 22
  • 33
16

what im using:

$seconds = strtotime("2010-10-20 08:10:00") - strtotime("2008-12-13 10:42:00");

$days    = floor($seconds / 86400);
$hours   = floor(($seconds - ($days * 86400)) / 3600);
$minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));

you can format now in your way

  • Thank you all for your valuable replay.with the help of above codes ,i got the answer $seconds = strtotime("2000-03-01 10:02:00") - strtotime("2000-02-28 10:02:00"); $days = floor($seconds / 86400); $hours = floor(($seconds - ($days * 86400)) / 3600); $minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60); $seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));$ff=$days*24;echo $time = $ff+$hours."hours".$minutes."minutes".$seconds."seconds"; – meginjoy May 22 '12 at 06:52
  • "The floor($seconds / 86400);" will fail on "summertime" dates unless time is specifically added to the date. Else it will be 1 hour off and finding the amount of dates will be 1 day too little (due to it being 1 hour short). Use DateTime and date_diff instead – Adam Touhou May 25 '17 at 15:33
4

You can use the the strtotime function to turn the time to integers and subtract them.

$time1 = strtotime("2008-12-13 10:42:00");
$time2 = strtotime("2010-10-20 08:10:00");

$diff = $time2-$time1;
// the difference in int. then you can divide by 60,60,24 and 
// so on to get the h:m:s out of it
Ibu
  • 42,752
  • 13
  • 76
  • 103