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