PHP 获取带微秒的时间戳和日期时间字符串

已邀请:

dave

赞同来自:

<?php
date_default_timezone_set('PRC');
$utimestamp = sprintf("%.6f", microtime(true)); // 带微秒的时间戳

$timestamp = floor($utimestamp); // 时间戳 
$microseconds = round(($utimestamp - $timestamp) * 1000000); // 微秒

$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $microseconds;
echo sprintf("%s -> %s", $utimestamp, $datetime);
//输出:1476674598.046942 -> 2016-10-17 11:23:18.46942

dave

赞同来自:

使用gettimeofday​()函数获取带微秒的日期时间和时间戳:
<?php
date_default_timezone_set('PRC');
$timeinfo = gettimeofday();

//带微秒的时间戳
$microsecond = sprintf("%s.d", $timeinfo["sec"], $timeinfo["usec"]);

//带微秒的日期时间
$datetime = sprintf("%s.d", date("Y-m-d H:i:s", $timeinfo["sec"]), $timeinfo["usec"]);

echo sprintf("%s -> %s", $microsecond, $datetime);
//输出:476674492.266350 -> 2016-10-17 11:21:32.266350

要回复问题请先登录注册