php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51737 Having a bug when using date_diff($t1,$t2)
Submitted: 2010-05-04 10:33 UTC Modified: 2010-05-04 12:07 UTC
From: iggukk at gmail dot com Assigned: aharvey (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.2 OS: Win XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: iggukk at gmail dot com
New email:
PHP Version: OS:

 

 [2010-05-04 10:33 UTC] iggukk at gmail dot com
Description:
------------
Hi,
Sorry if not posting this bug in the correct section. I'm currently having a bug when using date_diff().
Seems that when the "day" part of the dates is the same on both dates compared, the function returns "0 Days" instead of 30 or some Days.

Test script:
---------------
$datetime1 = date_create('2010-03-08');
$datetime2 = date_create('2010-04-08');
$interval = date_diff($datetime1,$datetime2);
echo $interval;
//=> returns "+0 Days"
//
//Whereas
//
$datetime1 = date_create('2010-03-08');
$datetime2 = date_create('2010-04-07');
$interval = date_diff($datetime1,$datetime2);
echo $interval;
//Will return "+30 Days"

Expected result:
----------------
Should return "a whole month" interval like +31 Days in that particular case.

Actual result:
--------------
returns +0 Days

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-04 10:57 UTC] aharvey@php.net
-Status: Open +Status: Feedback -Package: *General Issues +Package: Date/time related
 [2010-05-04 10:57 UTC] aharvey@php.net
The test script you've provided doesn't work, since echoing a DateInterval object isn't supported. How are you actually outputting $interval?
 [2010-05-04 11:04 UTC] iggukk at gmail dot com
-Status: Feedback +Status: Open
 [2010-05-04 11:04 UTC] iggukk at gmail dot com
Huh, sorry about that, and thanks for answering so quickly.

echo $interval->format('%R%d days');

simply from the example on php manual.

Just tried to feed the dates in different formats like 12.04.2010 and 12.03.2010, (instead of 2010-04-12) it seems to give the same result in final.
 [2010-05-04 11:11 UTC] aharvey@php.net
-Status: Open +Status: Assigned -Package: Date/time related +Package: Documentation problem -Assigned To: +Assigned To: aharvey
 [2010-05-04 11:11 UTC] aharvey@php.net
Not a bug, since you probably want %a (which is the total number of days in the interval) rather than %d.

%d only shows the number of days left over after date_diff() has calculated the number of months and years: in your first example, the interval is basically "1 month 0 days", so %d returns 0. (If you try "%m month(s) %d day(s)" as the format string, that will probably be a bit clearer.)

Nevertheless, the documentation could probably be a touch clearer on this point. Morphing into a documentation bug; I'll see if I can make it a bit more obvious in one of the examples what %d does.
 [2010-05-04 11:23 UTC] iggukk at gmail dot com
-Status: Assigned +Status: Closed
 [2010-05-04 11:23 UTC] iggukk at gmail dot com
Thanks a lot, aharvey you're great. 

I should have figured there was something to find in the format but my mind is kinda stuck this morning. 
Nevermind about the documentation. I should have guessed that there were more tags than %d. Like in all the date/time functions (doh!).
I guess I deserve a /slap for that.

Going back to work. Thanks again.
 [2010-05-04 12:07 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&revision=298955
Log: Added an example to make clearer the misunderstanding that led to bug #51737
(Having a bug when using date_diff($t1,$t2)).
 [2010-05-04 12:07 UTC] aharvey@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

Added an example anyway, just to be on the safe side. :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC