Bug #51184 [Com]: DateInterval has incorrect days property on windows

From: Date: Mon, 06 Feb 2012 21:25:02 +0000
Subject: Bug #51184 [Com]: DateInterval has incorrect days property on windows
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-167225@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=51184&edit=1

 ID:                 51184
 Comment by:         asdf at asdf dot com
 Reported by:        seld@php.net
 Summary:            DateInterval has incorrect days property on windows
 Status:             Wont fix
 Type:               Bug
 Package:            Date/time related
 Operating System:   Windows
 PHP Version:        5.3.2
 Assigned To:        pajoye
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2012-01-29 05:38:54] 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

------------------------------------------------------------------------
[2011-11-12 15:08:05] 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.

------------------------------------------------------------------------
[2011-10-16 16:13:05] 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-09-05 18:06:18] a at a dot com

Not solved with 5.3.5 on Windows.

------------------------------------------------------------------------
[2011-07-04 10:55:22] tux at penguinfriends dot org

Not solved with 5.3.5 on Windows...

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=51184


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=51184&edit=1


Thread (28 messages)

« previous php.bugs (#167225) next »