DateTime with microseconds

后端 未结 10 2109
长发绾君心
长发绾君心 2020-12-05 10:02

In my code, I\'m using DateTime objects to manipulate dates, then convert them to timestamp in order to save them in some JSON files.

For some reasons,

相关标签:
10条回答
  • 2020-12-05 10:37

    This worked for me in PHP 7.2:

    $dateTime = \DateTime::createFromFormat('U.u', sprintf('%f', $aFloat), $optionalTimezone);
    

    I got to thinking that since the format code 'u' would output only the microsecond part of a date when converting to a string then doing the reverse would be the same. And that it also expects a period character '.' so if $aFloat happened to be a whole number then default conversion to a string would leave off the decimal point. Initially I thought the float to string conversion needed '%.6f' but the 'u' is expecting a string which is left justified. Trailing zeros are unnecessary.

    0 讨论(0)
  • 2020-12-05 10:48
    $micro_seconds = microtime(false) * 1000000;
    echo date('Y-m-d H:i:s.'. floor($micro_seconds));
    

    more about date: https://www.php.net/manual/en/function.date.php

    more about microtime: https://www.php.net/manual/en/function.microtime.php

    0 讨论(0)
  • 2020-12-05 10:48

    Beautiful date and time, step by step:

    Note that the microtime() tells time AND microtime (numbers after the period)

    echo microtime(true) ."<br>"; //1601674357.9448
    sleep(0.99);
    echo microtime(true) ."<br>"; //1601674357.9449
    sleep(0.99);
    echo microtime(true) ."<br>"; //1601674357.945
    

    So let's take the numbers after the period:

    echo substr(microtime(true), 11,4) . "<br>"; //945
    

    But if for a moment you only had 1 or 2 digits after the period? We complete with zeros...

    Ok, now we always have 4 digits which are the microseconds

    echo str_pad(substr(microtime(true), 11,4), 4, '0', STR_PAD_RIGHT) . "<br>"; //9450
    

    So, let's add the date... Final result:

    $date = gmdate('Y-m-d h:i:s.');
    $time = str_pad(substr(microtime(true), 11,4), 4, '0', STR_PAD_RIGHT);
    
    echo $date . $time; //2020-10-02 09:43:57.9450
    
    0 讨论(0)
  • 2020-12-05 10:49

    Looking at a response on the PHP DateTime manual:

    DateTime does not support split seconds (microseconds or milliseconds etc.) I don't know why this isn't documented. The class constructor will accept them without complaint, but they are discarded. There does not appear to be a way to take a string like "2012-07-08 11:14:15.638276" and store it in an objective form in a complete way.

    So you cannot do date math on two strings such as:

    <?php
    $d1=new DateTime("2012-07-08 11:14:15.638276");
    $d2=new DateTime("2012-07-08 11:14:15.889342");
    $diff=$d2->diff($d1);
    print_r( $diff ) ;
    
    /* returns:
    
    DateInterval Object
    (
        [y] => 0
        [m] => 0
        [d] => 0
        [h] => 0
        [i] => 0
        [s] => 0
        [invert] => 0
        [days] => 0
    )
    
    */
    ?>
    

    You get back 0 when you actually want to get 0.251066 seconds.


    However, taking a response from here:

    $micro_date = microtime();
    $date_array = explode(" ",$micro_date);
    $date = date("Y-m-d H:i:s",$date_array[1]);
    echo "Date: $date:" . $date_array[0]."<br>";
    

    Recommended and use dateTime() class from referenced:

    $t = microtime(true);
    $micro = sprintf("%06d",($t - floor($t)) * 1000000);
    $d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
    
    print $d->format("Y-m-d H:i:s.u"); //note "u" is microseconds (1 seconds = 1000000 µs).
    

    Reference of dateTime() on php.net: http://php.net/manual/en/datetime.construct.php#

    0 讨论(0)
提交回复
热议问题