0

I want to get the time difference between the two times below. The format of the time difference should be hours minutes seconds and then micro seconds. I want the final answer in this format only.

$start = date(' H:i:s'.substr((string)microtime(), 1, 6));
//some code
$end = date(' H:i:s'.substr((string)microtime(), 1, 6));
//$interval = ???(end  time - start time)
trincot
  • 317,000
  • 35
  • 244
  • 286
BEDI
  • 41
  • 12
  • 3
    The example on the [microtime() documentation](http://php.net/manual/en/function.microtime.php) pretty much answers your question. You only need to format it the way you want. – Gerald Schneider Feb 09 '16 at 08:17

1 Answers1

0

This is answered by example #2 from the manual on microtime:

Example #2 Timing script execution in PHP 5

<?php
$time_start = microtime(true);
  
// Sleep for a while
usleep(100);

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";
?>

Combine this with this answer and you get:

$time_start = microtime(true);
   
// Do something here that takes time ...

$time_end = microtime(true);

$time = $time_end - $time_start;
$micro = sprintf("%06d", ($time - floor($time)) * 1000000);
$dateObj = new DateTime( date("Y-m-d H:i:s.$micro", $time) );

$interval = $dateObj->format("H:i:s.u");

If the time difference would be one minute, 23 seconds and 501293 micro seconds, then $difference will have this string value:

00:01:23.501293

Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
  • i want the time difference in the format given in the ques i.e is hours minutes and seconds and microseconds – BEDI Feb 12 '16 at 08:37