76

Possible Duplicates:
Number of seconds from now() to Sunday midnight
Calculates difference between two dates in PHP

Hello All,

In my project, I need to calculate the difference in seconds between two dates:

For Example :

$firstDay = "2011-05-12 18:20:20";
$secondDay = "2011-05-13 18:20:20";

Then I should get 86400 Seconds That is 24 hours.

Similarly For

$firstDay = "2011-05-13 11:59:20";
$secondDay = "2011-05-13 12:00:20";

It should return 60 Seconds.

I read lots of questions in the stackoverflow But they only deals with the difference between 2 minute fields like 11:50:01 and 12:10:57

Community
  • 1
  • 1
Pushpendra
  • 4,304
  • 5
  • 35
  • 64
  • Have a look at this article I wrote a couple of weeks ago. There's an example of this exact problem. http://webmonkeyuk.wordpress.com/2011/05/04/working-with-date-and-time-in-php/ – James C May 13 '11 at 07:23

1 Answers1

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

You will then be able to use the seconds to find minutes, hours, days, etc.

Gordon
  • 305,248
  • 71
  • 524
  • 547
Jordonias
  • 5,638
  • 2
  • 20
  • 32