note 116564 deleted from function.microtime by salathe
| From: | salathe@php.net | Date: | Fri, 06 Feb 2015 13:03:32 +0000 |
| Subject: | note 116564 deleted from function.microtime by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201612@lists.php.net to get a copy of this message | ||
Note Submitter: info at directwebsolutions dot nl
----
Today i became in a fight calculating the execution time in microseconds. The thing is that
webservers can be configured differently which causes different behavior on different webservers. I
never had a problem before until today.
I was fighting the problem that (end time) 1.3379 - 1.3378 (starting time) resulted into an
execution time of: 9.9999999999989E-5
It would be something with the precision which can be configured inside your php.ini or with
ini_set() and set the value 'precision' to 16. This didn't do the trick for me.
I also tried to make the number smaller (subtracting time() from microtime()) and so make the
calculation smaller. But this didn't make any difference to me.
The solution lies in some real easy function: number_format(). Just format your microtime(true)
float values using this function and let it work like you expect it to work:
<?php
$start_time = microtime(true);
$end_time = microtime(true);
$exec_time = number_format($end_time - $start_time, 10);
?>
For example:
<?php
$start_time = microtime(true); // 1421934216.024379
$end_time = microtime(true); // 1421934216.924662
$exec_time = number_format($end_time - $start_time, 10); // 0.9002830982
?>
I hope this helps someone!