views:

63

answers:

3

I have two dates in the format below:

Start Date = 30-10-2009

End Date = 30-11-2009

How, with PHP could I calculate the seconds between these two dates?

+2  A: 

The function strtotime() will convert a date to a unix-style timestamp (in seconds). You should then be able to subtract the end date from the start date to get the difference.

$difference_secs = strtotime($end_date) - strtotime($start_date);
drewm
+5  A: 

Parse the two dates into Unix timestamps using strtotime, then get the difference:

$firstTime = strtotime("30-10-2009");
$secondTime = strtotime("30-11-2009");
$diff = $secondtime - $firstTime;
Dominic Rodger
Can unix timestamps understand any format of date I throw in, for example: 2009-30-10 ?
Keith Donegan
It's pretty good - it shouldn't have problems with `2009-30-10`. You'll need to be careful with ambiguous dates (does "10-09-2009" mean 9th October or 10th September?).
Dominic Rodger
Yeah true, what would be the best format to put it in to avoid such clashes? - Thanks again
Keith Donegan
It doesn't really matter, provided you're consistent.
Dominic Rodger
+1  A: 

I'd rather advice to use built in DateTime object.

$firstTime = new DateTime("30-10-2009");
$diff = $firstTime->diff(new DateTime("30-11-2009"));

As for me it's more flexible and OOP oriented.

c0r0ner