|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65003 Wrong date diff
Submitted: 2013-06-10 12:08 UTC Modified: 2021-04-06 19:52 UTC
Avg. Score:4.4 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: hcy321+php at yandex dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.4.16 OS: OSX
Private report: No CVE-ID: None
 [2013-06-10 12:08 UTC] hcy321+php at yandex dot com
DateTime::diff return wrong result for first day of month.
Tested on PHP Version 5.4.10 \ 5.4.15

Test script:

$datetime1 = new DateTime('13-03-01');
$datetime2 = new DateTime('13-04-01');

$datetime3 = new DateTime('13-03-02');
$datetime4 = new DateTime('13-04-02');

$interval = $datetime2->diff($datetime1);
echo $interval->format('%m month, %d days'), '<br>'; //1 month, 3 days

$interval = $datetime4->diff($datetime3);
echo $interval->format('%m month, %d days'); //1 month, 0 days

Expected result:
1 month, 0 days
1 month, 0 days

Actual result:
1 month, 3 days
1 month, 0 days


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-11 12:03 UTC] ppasindud at gmail dot com
I had a similar error and check the src in intervel.c timelib_diff the date is changed to 
local or gmt



rt->d = two->d - one->d;

and makes it 1 month and 3 days 
could it be solved by only running timelib_apply_localtime if date zones are
different or else can put some other conditions to to prevent this, any other 
way to solve this, bug is for php 5.4 but exists in newer versions too.

 [2021-04-06 19:52 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2021-04-06 19:52 UTC]
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from and re-test.
Thank you for the report, and for helping us make PHP better.

Fixed for PHP 8.1.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 12:01:28 2024 UTC