与PHP中两个DateTime之间的微秒精度的差异

时间:2013-08-15 15:43:06

标签: php precision datediff

我可以在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或等效微秒。

任何人都知道解决方法吗?

6 个答案:

答案 0 :(得分:2)

使用微秒手动创建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对象的差异,然后再添加额外的微秒。

答案 1 :(得分:1)

自 PHP 7.1 (2016-12-01) 以来,微秒精度有 %f and %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 年回答我自己的问题

答案 2 :(得分:0)

我不得不替换这个

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

用这个

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

出于某种原因获得正确的microtime。

答案 3 :(得分: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"

答案 4 :(得分:0)

/**
 * 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);
}

答案 5 :(得分:0)

返回浮点数的更短版本

function diff(\DateTimeInterface $a, \DateTimeInterface $b): float
{
    return ($a->getTimestamp() - $b->getTimestamp()) + ($a->format("u") - $b->format("u")) / 1000000;
}