PHP DateTime microseconds always returns 0

前端 未结 18 2446
萌比男神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: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)

提交回复
热议问题