|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2016-12-29 19:25 UTC] for-bugs at hnw dot jp
 Description:
------------
From PHP 7.1, DateTime constructor fills microseconds. However, setting seconds and setting microseconds are in different function, so sometimes DateTime instance has 1 second ago value from actual time.
Test script:
---------------
<?php
$prev_dt = new DateTime();
while (true) {
    $dt = new DateTime();
    if ($prev_dt > $dt) {
        var_dump($prev_dt->format("Y-m-d H:i:s.u"));
        var_dump($dt->format("Y-m-d H:i:s.u"));
        break;
    }
    $prev_dt = $dt;
}
Expected result:
----------------
infinite loop
Actual result:
--------------
soon ends and outputs like this:
string(26) "2016-12-29 18:35:54.999998"
string(26) "2016-12-29 18:35:54.000000"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 04:00:01 2025 UTC | 
There's nothing curious. The current code uses different time-source against seconds and microseconds for the current time. They should be unified. In php_date_initialize(), timelib_unixtime2local() is called with time(NULL) as 2nd argument. In this function, unixtime is set to "now". Then php_date_set_current_time_fraction() is executed and the value from gettimeofday() is set as microsecond of "now". Suppose the accurate time is "2016-12-29 18:35:54.999999+00:00" when time(NULL) is called. Then time(NULL) returns 1483036554. 1 microsecond after, gettimeofday() is called and returns {1483036555, 0}. Then the value of "now" gets "148303654.000000" (Of course, it should be "1483036554.999999" or "1483036555.000000").