Jump to content

wrong result from DateTime::diff()


amelio

Recommended Posts

I copied and pasted the code below from php manual, http://www.php.net/manual/en/datetime.diff.php, I should have got the result of '2' as they did but I got the result of '6015'.

 

I can't think why this would be. Any help appreciated.

 

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Link to comment
https://forums.phpfreaks.com/topic/224610-wrong-result-from-datetimediff/
Share on other sites

Did you look at the comments for some of the workarounds for PHP versions less than 5.3?

 

I actually have 5.3 installed on my WAMP set up, but I still get the same error that you're getting. It's very weird..

 

Let us know if any of the work arounds do the trick for you

 

Denno

I found a listing that gets the right answer...

 

$date1 = new DateTime("2009-10-11"); 
$date2 = new DateTime("2009-10-13"); 
$interval = $date1->diff($date2); 
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";

 

I'm not sure how 'm' gives the months remaining after the years have been calculated and 'd' also knows the remainder of days after months have been accounted for but it works that's the main thing. I'm wondering if the original listing didn't work because I am on a windows machine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.