php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69211 DateTime Diff is off by 3
Submitted: 2015-03-10 12:40 UTC Modified: 2015-03-10 13:12 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: lr at cwd dot at Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.4.38 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: lr at cwd dot at
New email:
PHP Version: OS:

 

 [2015-03-10 12:40 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-10 12:43 UTC] lr at cwd dot at
-Summary: DateTime Diff is off by 2 +Summary: DateTime Diff is off by 3
 [2015-03-10 12:43 UTC] lr at cwd dot at
summary fix
 [2015-03-10 13:12 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-03-10 13:12 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 11:00:02 2026 UTC