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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.