|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 00:00:02 2025 UTC |
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.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; }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; }