php.net |  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
Votes:7
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
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: hcy321+php at yandex dot com
New email:
PHP Version: OS:

 

 [2013-06-10 12:08 UTC] hcy321+php at yandex dot com
Description:
------------
DateTime::diff return wrong result for first day of month.
Tested on PHP Version 5.4.10 \ 5.4.15

Test script:
---------------
<?php
date_default_timezone_set('Europe/Moscow');

$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

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

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

from
13-03-01
13-04-01

to
13-02-28
13-03-31

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.

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

Fixed for PHP 8.1.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed May 12 18:01:23 2021 UTC