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

 ID:                 51184
 Comment by:         asdf at asdf dot com
 Reported by:        s...@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:

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;
        }


Previous Comments:
------------------------------------------------------------------------
[2012-02-06 21:25:02] 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-01-29 05:38:54] alabi10 at yahoo dot com

The fix submitted by fbast...@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.

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


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

Reply via email to