Domanda

I can get a datetime with microseconds in PHP with a workaround as:

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

I need the difference with microseconds between two datetimes, can't get a workaround for:

$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 doesn't has the format character %u or equivalent for microseconds.

Anyone knows a workaround for this?

È stato utile?

Soluzione 2

Manually creating a DateTime object with micro seconds:

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

Getting a DateTime object of the current time with microseconds:

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

Difference between two DateTime objects in microseconds (e.g. returns: 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;
}

Essentially it finds the difference for the DateTime Objects using strtotime and then adding the extra microseconds on.

Altri suggerimenti

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

Since PHP 7.1 (2016-12-01) there is %f and %F for microseconds precision, but just from 7.2 (2017-11-30) it is safe to use due to the critical date bugs in 7.1 (Tested)

The working example:

<?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: Answering my own question posted 4 years before the existence of %f

I had to replace this

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

with this

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

to get the correct microtime for some reason.

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;
}

This function returns the correct 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"

Shorter version with float returned

function diff(\DateTimeInterface $a, \DateTimeInterface $b): float
{
    return ($a->getTimestamp() - $b->getTimestamp()) + ($a->format("u") - $b->format("u")) / 1000000;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top