edoceo: Latin "to inform fully, instruct thoroughly"

PHP microtime difference function


The function below will calculate the difference between two different microtime values. $old_mt should be the older of the two (if known), $new_mt of course represents the newer of the two. If $old_mt is actually a newer time than $new_mt the return value will be negative.

Here's a goofy usage example

<?php
$_start = microtime();
// code whole script here
echo "Took ".number_format(diff_microtime($_start,microtime()), 4)." seconds to execute\n";
?>

The diff_microtime Function

You may copy and paste this code freely

// diff_microtime *********************************************************
// Calculate the difference between two different microtimes
// I like this better than the `getmicrotime()` option on PHP.net
function diff_microtime($mt_old,$mt_new)
{
  list($old_usec, $old_sec) = explode(' ',$mt_old);
  list($new_usec, $new_sec) = explode(' ',$mt_new);
  $old_mt = ((float)$old_usec + (float)$old_sec);
  $new_mt = ((float)$new_usec + (float)$new_sec);
  return $new_mt - $old_mt;
}
XHTML 1.1. and CSS2.1/3 © 1999-2008 Edoceo, Inc.
Edit this Page