Bug #69211 [Opn->Nab]: DateTime Diff is off by 3

From: Date: Tue, 10 Mar 2015 13:12:43 +0000
Subject: Bug #69211 [Opn->Nab]: DateTime Diff is off by 3
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191284@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69211&edit=1 ID: 69211 Updated by: requinix@php.net Reported by: lr at cwd dot at Summary: DateTime Diff is off by 3 -Status: Open +Status: Not a bug Type: Bug Package: Date/time related Operating System: Linux PHP Version: 5.4.38 Block user comment: N Private report: N New Comment: It has nothing to do with the year. The problem is how to handle December 31st + 2 months because February 31st isn't a real date: a) February 28th because it's the last day of the month. But you'll get the same result with December 30th, 29th, and 28th. b) Feburary "31st", which overflows and becomes March 3rd. This is what PHP is doing here, and in fact does everywhere else too. Thus a difference of 2M 7D. Neither choice is particularly good, but PHP has to make one and the second is better. Previous Comments: ------------------------------------------------------------------------ [2015-03-10 12:43:20] lr at cwd dot at summary fix ------------------------------------------------------------------------ [2015-03-10 12:40:51] lr at cwd dot at Description: ------------ DateTime::diff() ist off by 3 days when year roll over Test script: --------------- <?php date_default_timezone_set('UTC'); $datetime1 = new DateTime('2015-01-01'); $datetime2 = new DateTime('2015-03-10'); $interval = $datetime1->diff($datetime2); echo $interval->format('%y years %m months and %d days')."\n"; $datetime1 = new DateTime('2014-12-31'); $datetime2 = new DateTime('2015-03-10'); $interval = $datetime1->diff($datetime2); echo $interval->format('%y years %m months and %d days')."\n"; $datetime1 = new DateTime('1951-12-31'); $datetime2 = new DateTime('2015-03-10'); $interval = $datetime1->diff($datetime2); echo $interval->format('%y years %m months and %d days')."\n"; Expected result: ---------------- 0 years 2 months and 9 days 0 years 2 months and 10 days 63 years 2 months and 10 days Actual result: -------------- 0 years 2 months and 9 days 0 years 2 months and 7 days 63 years 2 months and 7 days ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69211&edit=1

« previous php.bugs (#191284) next »