7

我可以通过以下解决方法在 PHP 中获得微秒的日期时间:

list($usec, $sec) = explode(" ", microtime());
echo date("Y-m-d\TH:i:s", $sec) . "." . floatval($usec)*pow(10,6);

我需要两个日期时间之间的微秒差异,无法解决以下问题:

$datetime1 = new DateTime('2013-08-14 18:49:58.606');
$datetime2 = new DateTime('2013-08-14 22:27:19.272');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h hours %i minutes %s seconds %u microseconds');

DateInterval::format在微秒内没有格式字符 %u 或等效字符。

有人知道解决方法吗?

4

6 回答 6

5
/**
 * returns the difference in seconds.microseconds(6 digits) format between 2 DateTime objects 
 * @param DateTime $date1
 * @param DateTime $date2
 */
function mdiff($date1, $date2){
    return number_format(abs((float)$date1->format("U.u") - (float)$date2->format("U.u")), 6);
}
于 2018-05-21T00:37:52.643 回答
4

用微秒手动创建一个 DateTime 对象:

$d = new DateTime("15-07-2014 18:30:00.111111");

以微秒获取当前时间的 DateTime 对象:

$d = date_format(new DateTime(),'d-m-Y H:i:s').substr((string)microtime(), 1, 8);

以微秒为单位的两个 DateTime 对象之间的差异(例如返回:2.218939)

//Returns the difference, in seconds, between two datetime objects including
//the microseconds:

function mdiff($date1, $date2){
//Absolute val of Date 1 in seconds from  (EPOCH Time) - Date 2 in seconds from (EPOCH Time)
$diff = abs(strtotime($date1->format('d-m-Y H:i:s.u'))-strtotime($date2->format('d-m-Y H:i:s.u')));

//Creates variables for the microseconds of date1 and date2
$micro1 = $date1->format("u");
$micro2 = $date2->format("u");

//Absolute difference between these micro seconds:
$micro = abs($micro1 - $micro2);

//Creates the variable that will hold the seconds (?):
$difference = $diff.".".$micro;

return $difference;
}

本质上,它使用 strtotime 找到 DateTime 对象的差异,然后添加额外的微秒。

于 2014-07-15T14:59:50.750 回答
1

由于 PHP 7.1 (2016-12-01) 有%f 和 %F用于微秒精度,但仅从 7.2 (2017-11-30) 开始,由于 7.1 中的关键日期错误(已测试),使用它是安全的

工作示例:

<?php
$datetime1 = new DateTime('2013-08-14 18:49:58.800');
$datetime2 = new DateTime('2013-08-14 22:27:19.900');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h hours %i minutes %s seconds %f microseconds');

PD:回答我自己在 %f 存在前 4 年发布的问题

于 2021-01-30T08:13:49.907 回答
0

我不得不更换这个

$micro = abs($micro1 - $micro2);

有了这个

str_pad(abs($micro1 - $micro2), 6, '0', STR_PAD_LEFT);

出于某种原因获得正确的微时间。

于 2016-06-09T19:43:26.823 回答
0
function mdiff($date1, $date2){
  //Absolute val of Date 1 in seconds from  (EPOCH Time) - Date 2 in seconds from (EPOCH Time)
  $diff = abs(strtotime($date1->format('d-m-Y H:i:s.u'))-strtotime($date2->format('d-m-Y H:i:s.u')));

  //Creates variables for the microseconds of date1 and date2
  $micro1 = $date1->format("u");
  $micro2 = $date2->format("u");

  //Difference between these micro seconds:
  $diffmicro = $micro1 - $micro2;

  list($sec,$micro) = explode('.',((($diff) * 1000000) + $diffmicro )/1000000);

  //Creates the variable that will hold the seconds (?):
  $difference = $sec . "." . str_pad($micro,6,'0');

  return $difference;
}

此函数返回正确的差异

Example:
    Start:"2016-10-27 17:17:52.576801"
    End:"2016-10-27 17:18:00.385801"
    Difference:"7.809000"

    Old Function:
    Difference:"8.191000"
于 2016-10-27T15:12:54.893 回答
0

带有浮点数的较短版本返回

function diff(\DateTimeInterface $a, \DateTimeInterface $b): float
{
    return ($a->getTimestamp() - $b->getTimestamp()) + ($a->format("u") - $b->format("u")) / 1000000;
}
于 2020-07-28T06:52:37.587 回答