Last active
March 5, 2021 04:05
-
-
Save hadl/5721816 to your computer and use it in GitHub Desktop.
PHP Microtime Diff -- Calculate a precise time difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 flat difference in seconds, calculated with minimum precision loss | |
*/ | |
function microtime_diff($start, $end = null) | |
{ | |
if (!$end) { | |
$end = microtime(); | |
} | |
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