php.net |  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
Votes:18
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:
 [2009-05-08 05:42 UTC] wavetrex at gmail dot com
Description:
------------
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:
---------------
<?php
// two arbitrary dates
$date1=new DateTime("2005-07-23");
$date2=new DateTime("2006-02-14");

echo "<pre>";
var_dump(microtime());
var_dump(time());
var_dump(date(DATE_ATOM));

var_dump($date1->diff($date2));

var_dump(microtime());
var_dump(time());
var_dump(date(DATE_ATOM));
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"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

<?php
// 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($date1->diff($date2));

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
@jani:

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

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:
http://wt.ath.cx/diff.php
The code for this script is here:
http://wt.ath.cx/diff.txt

Server configuration:
http://wt.ath.cx/phpinfo.php

Time difference between the two calls:

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

Your script:
http://wt.ath.cx/jani.php
... 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)

P.S.
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 
values.

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] jani@php.net
What is the output of the script I provided?
 [2009-05-10 01:41 UTC] wavetrex at gmail dot com
float(1241919300.3593)
float(1241709350.3736)
int(1241919300)
int(1241919300)
string(25) "2009-05-10T04:35:00+03:00"
string(25) "2009-05-10T04:35:00+03:00"

( http://wt.ath.cx/jani.php )

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] k.schroeder@php.net
Automatic comment from SVN on behalf of k.schroeder
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=301359
Log: Test for #48187
 [2010-08-10 07:01 UTC] jasonjoo dot god at gmail dot com
my test script below:
<?php 
date_default_timezone_set("Asia/Chongqing");
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:
1281415176.9756
1281402595.446
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] stas@php.net
-Status: Open +Status: Feedback
 [2011-01-30 11:26 UTC] stas@php.net
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-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 12:01:52 2014 UTC