views:

151

answers:

3

Is there a quick way to calculate date difference in php? For example:

$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';

And then do a calculation, $date2 minus $date1

I read php.net documentation, but no luck. Is there a quick way to do it?

EDIT:

I will put up the whole answer (for some people who might curious of how to do it)

$date1 = '2009-12-20 20:12:10';
$date2 = '2009-12-24 12:12:10';

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;

echo floor($seconds_diff/3600/24);

Result return in "days", you can get your desired result by playing the $seconds_diff, which is the difference in seconds

+1  A: 

http://www.php.net/manual/en/datetime.diff.php

Josh Barker
This function requires php 5.3 or better
txyoji
+5  A: 

is the answer not to be found here?

Xav
That would be a better answer. Nice find.
txyoji
Sorry, I am new to this site. Didnt know this question has been posted. Somebody can help me closed this question, I cant do it.
mysqllearner
I just used the "Search" box above and looked for "date diff php" ;-)
Xav
Ok, learned a lesson.
mysqllearner
By the way, the answer in the post referenced here has small mistake. When you have to deal with summer/winter time, one day may equals to 23 or 25 hours.
Arno
+3  A: 

strtotime will convert your date string to a unix time stamp. (seconds since the unix epoch.

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;
txyoji
Do you mind if I put your answer to my question? (I wrote the whole code)
mysqllearner