Skip to content

Instantly share code, notes, and snippets.

@joel-depiltech
Forked from hadl/microtime_diff.php
Last active October 3, 2017 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joel-depiltech/413fd523b8120ad252552f059352d813 to your computer and use it in GitHub Desktop.
Save joel-depiltech/413fd523b8120ad252552f059352d813 to your computer and use it in GitHub Desktop.
PHP Microtime Diff -- Calculate a precise time difference
<?php
/**
* Calculate a precise time difference.
* @param string $start result of microtime()
* @param string $end result of microtime(); if NULL/FALSE/0/'' then it's now
* @return float difference in seconds, calculated with minimum precision loss
*/
function microtime_diff($start, $end = NULL)
{
$end = empty($end) ? microtime() : $end;
list($start_usec, $start_sec) = explode(" ", $start);
list($end_usec, $end_sec) = explode(" ", $end);
$diff_sec = intval($end_sec) - intval($start_sec);
$diff_usec = floatval($end_usec) - floatval($start_usec);
return floatval($diff_sec) + $diff_usec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment