2

lets say i have two input, date 1 and date2 somehow i need the difference in seconds

Thu, 26 May 2011 11:40:00 +0000 
Thu, 26 May 2011 11:39:40 +0000 
===============================
                     120 minutes x 60 + 20 seconds

or

Thu, 26 May 2011 11:40:00 +0000
Thu, 25 May 2011 11:39:40 +0000
===============================
                     120 minutes + 24 hours ( one day ) / 60x24 = 1440 minutes
                     = 1560 minutes x 60 + 20 seconds

so on..

how can we do something like above using php?

*edit forgot about the secs.

Thanks for looking in!.

Adam Ramadhan

Adam Ramadhan
  • 22,712
  • 28
  • 84
  • 124

2 Answers2

2

Use the DateTime class. Note that diff() is a >= PHP 5.3 method. If you have an older version, you can always use strtotime().

$dateStart = new DateTime('Thu, 26 May 2011 11:40:00 +0000');

$dateFinish = new DateTime('Thu, 25 May 2011 11:39:40 +0000');

echo $dateStart->diff($dateFinish)->s; // 20

CodePad.

alex
  • 479,566
  • 201
  • 878
  • 984
2
$diff = strtotime($date2) - strtotime($date2);
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143