|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48187 DateTime::diff() corrupting microtime() result
Submitted: 2009-05-08 05:42 UTC Modified: 2013-02-18 01:26 UTC
Avg. Score:4.3 ± 0.9
Reproduced:16 of 17 (94.1%)
Same Version:4 (25.0%)
Same OS:6 (37.5%)
From: wavetrex at gmail dot com Assigned:
Status: Closed Package: Date/time related
PHP Version: 5.3.0RC2 OS: Windows 2003 Server
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: wavetrex at gmail dot com
New email:
PHP Version: OS:


 [2009-05-08 05:42 UTC] wavetrex at gmail dot com
calling DateTime::diff() seems to corrupt the result returned by microtime(), so it returns the wrong time after this function has been called

Reproduce code:
// two arbitrary dates
$date1=new DateTime("2005-07-23");
$date2=new DateTime("2006-02-14");

echo "<pre>";


echo "</pre>";


Expected result:
Expecting the 2nd microtime to be slightly bigger ( by a few miliseconds ) than first.

instead, it's a totally wrong 50000 seconds distance (or maybe other arbitrary value)

date() and time() seem to be unaffected

Actual result:
before calling diff:
string(21) "0.98425000 1241761004"

after it:
string(21) "0.25882200 1241709345"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-09 04:15 UTC]
Try this script instead: (I can't reproduce this..)

// two arbitrary dates
$date1=new DateTime("2005-07-23");
$date2=new DateTime("2006-02-14");

$begin_u = microtime(true);
$begin_t = time();
$begin_a = date(DATE_ATOM);


var_dump($begin_u, microtime(true));
var_dump($begin_t, time());
var_dump($begin_a, date(DATE_ATOM));

 [2009-05-09 06:58 UTC] wavetrex at gmail dot com

I'm using microtime() multiple times to determine how long some 
complex parts of the script take to execute, when I noticed weird 

Also, while the second microtime() call after diff() is wrong, the 
third seems to be ok, just like calling the function resets some 
internal variable that was broken during execution of diff()

I prepared a script on my server:
The code for this script is here:

Server configuration:

Time difference between the two calls:

Expected result:
3.814697265625E-5 (a few microseconds)
Actual result:
-142455.45035195 (??)

Your script:
... also has the error.

If this probably occurs on my server, I'm thinking... it might be 
timezone related ? I'm sure yours and mine are different. (check my 
phpinfo.php to see date/time settings)

While investigating, I noticed that in some rare cases it doesn't 
happen, and after Ctrl-R it gives the erroned value again. Try 
reloading the page a few times in case 1st time it shows the correct 

I'll keep experimenting with the new 5.3.0 time functions and see if 
some other errors appear.
 [2009-05-09 20:59 UTC]
What is the output of the script I provided?
 [2009-05-10 01:41 UTC] wavetrex at gmail dot com
string(25) "2009-05-10T04:35:00+03:00"
string(25) "2009-05-10T04:35:00+03:00"

( )

Note: 1241709350 -> this value seems to stay almost unchanged over time 
(look at my first submission: string(21) "0.25882200 1241709345" )

When I reported the difference was ~50000 seconds, now it's over 200.000
 [2010-07-18 00:47 UTC]
Automatic comment from SVN on behalf of k.schroeder
Log: Test for #48187
 [2010-08-10 07:01 UTC] jasonjoo dot god at gmail dot com
my test script below:
echo microtime(true);
echo "\n";
$now=new DateTime();
$now->diff(new DateTime());
echo microtime(true);
and my os is win32/xp
I found this isue can be reproduced under apache in handler mode, but it is no isue in command line mode(eg. php test.php).

for example, I got its output like this:
and the wrong one only change the part following dot

my php version is 5.3.3
 [2010-08-13 20:59 UTC] alpha_centurion at hotmail dot com
Reproduced on Win32 XP SP3 with Apache 2.2.15 and php 5.3.2-Win32-VC6-x86

Only seems to impact web requests as same script (by jani) does not evidence the same error when run on the command line.
 [2011-01-30 11:26 UTC]
-Status: Open +Status: Feedback
 [2011-01-30 11:26 UTC]
Is it VC6-based build? If yes, could you try a VC9-based build instead?
 [2013-02-18 00:33 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 [2013-02-18 01:26 UTC] wavetrex at gmail dot com
-Status: No Feedback +Status: Closed
 [2013-02-18 01:26 UTC] wavetrex at gmail dot com
This is a very old bug ;)
It appears that in my current version on the same server 5.3.20, it's not occurring anymore.

Using PHP VC9 compiled executable now on Apache VC9 running as a module.

Who knows what strange overflow caused it back then. There have been many versions since and it appears that it has been solved with one of them.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Feb 29 16:01:38 2024 UTC