php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51184 DateInterval has incorrect days property on windows
Submitted: 2010-03-02 13:41 UTC Modified: 2011-01-03 11:02 UTC
Votes:158
Avg. Score:4.4 ± 0.8
Reproduced:143 of 144 (99.3%)
Same Version:69 (48.3%)
Same OS:133 (93.0%)
From: seld@php.net Assigned: pajoye
Status: Wont fix Package: Date/time related
PHP Version: 5.3.2 OS: Windows
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-03-02 13:41 UTC] seld@php.net
Description:
------------
On Windows, no matter what the dates are, the "days" property of the DateInterval object returned by DateTime::diff() is always 6015.

Might be related to http://bugs.php.net/bug.php?id=49778 and http://bugs.php.net/bug.php?id=49081

Test script:
---------------
$start = new DateTime('2010-06-06'); 
$end   = new DateTime('2011-02-04'); 
echo $start->diff($end)->days;

$start = new DateTime('2005-01-01'); 
echo $start->diff($end)->days;

Expected result:
----------------
The right number of days, or at least always 42 :)

Actual result:
--------------
6015, always.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-25 13:26 UTC] kalle@php.net
-Status: Open +Status: Feedback
 [2010-03-25 13:26 UTC] kalle@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

Derick fixed #49778, did that fix the issue?
 [2010-05-19 11:38 UTC] pluk77 at gmail dot com
There seems to be no windows snapshot available so unable to test if the bugfix solved this problem as well.
 [2010-05-19 11:43 UTC] pajoye@php.net
It has been released, so try 5.3.2
 [2010-05-19 11:54 UTC] seld@php.net
-Status: Feedback +Status: Closed -Assigned To: +Assigned To: seld
 [2010-05-19 11:54 UTC] seld@php.net
Confirmed working here. Kalle's comment came after 5.3.2 and I thought the commit was younger too, but it seems good in 5.3.2 indeed.
 [2010-05-19 15:20 UTC] pluk77 at gmail dot com
I am using this version:

PHP5.3.2 VC6 x86 Thread Safe (2010-Mar-04 20:11:08)

and the bug still shows.

Can anybody else confirm this bug is still open?
 [2010-05-19 15:22 UTC] seld@php.net
Can you try with VC9? Works here on latest VC9 Non Thread Safe
 [2010-05-19 15:45 UTC] pluk77 at gmail dot com
Done so. Tested the VC6 and VC9 builds. The VC6 build still has this bug while the VC9 does not.
 [2010-05-19 16:20 UTC] seld@php.net
VC6 NTS fails as well here. VC9 NTS works.
 [2010-05-19 16:21 UTC] seld@php.net
-Status: Closed +Status: Re-Opened -PHP Version: 5.3.2RC3 +PHP Version: 5.3.2 -Assigned To: seld +Assigned To: pajoye
 [2010-06-04 22:40 UTC] randy dot larson at veracitynetworks dot com
I have with PHP 5.3.2 on Cent OS 5.4 running the following code from the CLI:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-11-13');
$interval = $datetime1->sub($datetime2);
echo $interval->format('%R%d days');

This prints "+2 days" where it should print "+33 days". It appears that it doesn't add the month.
 [2010-06-05 04:14 UTC] ric dot lombardi at gmail dot com
randy, this is not a bug. the format %d just shows the difference between the two 
day numbers.
If you want the absolute days difference between the dates, you have to use "%a" 
as format.
 [2010-06-14 12:32 UTC] pajoye@php.net
-Status: Re-Opened +Status: Closed
 [2010-06-14 12:43 UTC] pluk77 at gmail dot com
Is this bug closed because it is now fixed in VC6 as well, or because it is seen as not a bug?
 [2010-06-14 12:50 UTC] pajoye@php.net
-Status: Closed +Status: Wont fix
 [2010-06-14 12:50 UTC] pajoye@php.net
Sorry, I meant won't fix. It is not something we can easily fix, use the VC9 versions instead (apachelounge.com for the VC9 versions of apache).
 [2010-10-31 02:13 UTC] php at twinmail dot de
5.3.3 on WINNT is also affected.
 [2010-10-31 03:32 UTC] pajoye@php.net
@php at twinmail dot de

Use VC9 builds instead, from http://windows.php.net
 [2011-01-03 10:42 UTC] toto at hotmail dot com
Not solved with PHP 5.3.4 (Windows / Apache 2)
 [2011-01-03 11:02 UTC] pajoye@php.net
@toto at hotmail dot com

Nothing changed so yes, use VC9 builds instead for now.
 [2011-07-04 10:55 UTC] tux at penguinfriends dot org
Not solved with 5.3.5 on Windows...
 [2011-09-05 18:06 UTC] a at a dot com
Not solved with 5.3.5 on Windows.
 [2011-10-16 16:13 UTC] fbastage at yahoo dot com
Here's a reasonably close substitute  (run result through abs() if you don't 
want potentially negative numbers):

// $dt1 and $dt2 can be any valid date string that DateTime accepts
// the time zone shouldn't affect anything (since $dt1 and $dt2 use same zone),
// but you can override the default
function daysdiff($dt1, $dt2, $timeZone = 'America/Chicago') {
  $tZone = new DateTimeZone($timeZone);
  
  $dt1 = new DateTime($dt1, $tZone);
  $dt2 = new DateTime($dt2, $tZone);  
  
  // use the DateTime datediff function IF we have a non-buggy version
  // there is a bug in many Windows implementations that diff() always returns
  // 6015  
  if( $dt1->diff($dt1)->format("%a") != 6015 ) {
    return $dt1->diff($dt2)->format("%a");
  }
  
  // else let's use our own method

  $y1 = $dt1->format('Y');  
  $y2 = $dt2->format('Y');
  $z1 = $dt1->format('z');
  $z2 = $dt2->format('z');
  
  $diff = intval($y1 * 365.2425 + $z1) - intval($y2 * 365.2425 + $z2);

  return $diff;

}
 [2011-11-12 15:08 UTC] iskeen at barebrush dot com
I am surprised that the number of days between two dates problem is not made clear right up front in the documentation. It took me 3 hours to find this page and I was trying all different things to make it work. I suspected a problem when the three different sets of dates I was using all came out with the same answer, but when I finally used %a and they all came out with 6015, I knew, finally, that there is a problem. The fact of the problem should be made very clear right up front.
 [2012-01-29 05:38 UTC] alabi10 at yahoo dot com
The fix submitted by fbastage@yahoo.com on 2011-10-16 16:13 UTC solved the problem 
for me on Windows 7 running WAMP on localhost and php 5.3.0
 [2012-02-06 21:25 UTC] asdf at asdf dot com
Also have this problem. A full year since last modified. No fixes yet? 

I agree with the other use who said there should be more documentation on this error. I am sure lots of us spent lots of time before we found this thread trying to figure out why diff() does not output the correct result.

Right here would be a great place to put some documentation on the 6015 error: http://us3.php.net/manual/en/datetime.diff.php
 [2012-02-06 21:45 UTC] asdf at asdf dot com
Your function can be changed to accept Datetime Objects for Strings this way. Also now calculates datetime difference from dt1 to dt2 rather then the other way around.

private function daysdiff($dt1, $dt2, $timeZone = 'America/Chicago') 
	{
	  $tZone = new DateTimeZone($timeZone);
	  
	  if(is_string($dt1))
	  {
	  	$dt1 = new DateTime($dt1, $tZone);
	  }
	  if(is_string($dt2))
	  {
		$dt2 = new DateTime($dt2, $tZone);  
	  }
	  
	  // use the DateTime datediff function IF we have a non-buggy version
	  // there is a bug in many Windows implementations that diff() always returns 6015  
	  if( $dt1->diff($dt1)->format("%a") != 6015 ) {
	    return $dt1->diff($dt2)->format("%a");
	  }
	  
	  // else let's use our own method
	  $y1 = $dt1->format('Y');  
	  $y2 = $dt2->format('Y');
	  $z1 = $dt1->format('z');
	  $z2 = $dt2->format('z');
	  
	  $diff = intval($y2 * 365.2425 + $z2) - intval($y1 * 365.2425 + $z1);
	  return $diff;
	}
 [2012-03-25 12:47 UTC] test dot php at yopmail dot com
Still have this bug on the 5.3.5 version more than 2 years later.
It is not acceptable to have a "won't fix" decision on a major bug like this that is affecting so many people.
Thank you in advance,
 [2012-03-25 12:56 UTC] seld@php.net
Everyone please read the full thread before complaining, it's a bug affecting VC6 builds only. If you are affected just get VC9 builds from http://windows.php.net/download/ - you will also most likely need a VC9 or VC10 build of Apache from http://www.apachelounge.com/download/

VC6 is very old, outdated and not supported anymore, it's easy enough and free to upgrade to newer builds.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 12:01:52 2014 UTC