79

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 on Stack Overflow but they only deal with the difference between 2 minute fields like 11:50:01 and 12:10:57

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pushpendra
  • 4,344
  • 5
  • 36
  • 64

1 Answers1

179
$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
  • 312,688
  • 75
  • 539
  • 559
Jordonias
  • 5,778
  • 2
  • 21
  • 32
  • Thanks, I used this on a form that was getting spammed. I compare the time stamps in seconds (hidden field with form timestamp and then php when the form is submitted), if it's too fast like 5 seconds then don't submit the form. – drooh Jun 30 '23 at 00:56