PHP DateTime microseconds always returns 0

前端 未结 18 2405
萌比男神i
萌比男神i 2020-12-01 02:55

this code always returns 0 in PHP 5.2.5 for microseconds:

format(\"Y-m-d\\TH:i:s.u\") . \"\\n\";
?>

相关标签:
18条回答
  • 2020-12-01 03:03

    Right, I'd like to clear this up once and for all.

    An explanation of how to display the ISO 8601 format date & time in PHP with milliseconds and microseconds...

    milliseconds or 'ms' have 4 digits after the decimal point e.g. 0.1234. microseconds or 'µs' have 7 digits after decimal. Seconds fractions/names explanation here

    PHP's date() function does not behave entirely as expected with milliseconds or microseconds as it will only except an integer, as explained in the php date docs under format character 'u'.

    Based on Lucky's comment idea (here), but with corrected PHP syntax and properly handling seconds formatting (Lucky's code added an incorrect extra '0' after the seconds)

    These also eliminate race conditions and correctly formats the seconds.

    PHP Date with milliseconds

    Working Equivalent of date('Y-m-d H:i:s').".$milliseconds";

    list($sec, $usec) = explode('.', microtime(true));
    echo date('Y-m-d H:i:s.', $sec) . $usec;
    

    Output = 2016-07-12 16:27:08.5675

    PHP Date with microseconds

    Working Equivalent of date('Y-m-d H:i:s').".$microseconds"; or date('Y-m-d H:i:s.u') if the date function behaved as expected with microseconds/microtime()/'u'

    list($usec, $sec) = explode(' ', microtime());
    echo date('Y-m-d H:i:s', $sec) . substr($usec, 1);
    

    Output = 2016-07-12 16:27:08.56752900

    0 讨论(0)
  • 2020-12-01 03:03

    This has worked for me and is a simple three-liner:

    function udate($format='Y-m-d H:i:s.', $microtime=NULL) {
        if(NULL === $microtime) $microtime = microtime();
        list($microseconds,$unix_time) = explode(' ', $microtime);
        return date($format,$unix_time) . array_pop(explode('.',$microseconds));
    }
    

    This, by default (no params supplied) will return a string in this format for the current microsecond it was called:

    YYYY-MM-DD HH:MM:SS.UUUUUUUU

    An even simpler/faster one (albeit, with only half the precision) would be as follows:

    function udate($format='Y-m-d H:i:s.', $microtime=NULL) {
        if(NULL === $microtime) $microtime = microtime(true);
        list($unix_time,$microseconds) = explode('.', $microtime);
        return date($format,$unix_time) . $microseconds;
    }
    

    This one would print out in the following format:

    YYYY-MM-DD HH:MM:SS.UUUU

    0 讨论(0)
  • 2020-12-01 03:03

    String in a format accepted by strtotime() It work!

    0 讨论(0)
  • 2020-12-01 03:04

    Try this and it shows micro seconds:

    $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");
    
    0 讨论(0)
  • 2020-12-01 03:04

    PHP documentation clearly says "Note that date() will always generate 000000 since it takes an integer parameter...". If you want a quick replacement for date() function use below function:

    function date_with_micro($format, $timestamp = null) {
        if (is_null($timestamp) || $timestamp === false) {
            $timestamp = microtime(true);
        }
        $timestamp_int = (int) floor($timestamp);
        $microseconds = (int) round(($timestamp - floor($timestamp)) * 1000000.0, 0);
        $format_with_micro = str_replace("u", $microseconds, $format);
        return date($format_with_micro, $timestamp_int);
    }
    

    (available as gist here: date_with_micro.php)

    0 讨论(0)
  • 2020-12-01 03:04

    date('u') is supported only from PHP 5.2. Your PHP may be older!

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