note 116564 added to function.microtime

From: Date: Thu, 22 Jan 2015 13:52:10 +0000
Subject: note 116564 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-201500@lists.php.net to get a copy of this message
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! ---- Server IP: 72.52.91.14 Probable Submitter: 82.161.140.36 ---- Manual Page -- http://php.net/manual/en/function.microtime.php Edit -- https://master.php.net/note/edit/116564 Del: integrated -- https://master.php.net/note/delete/116564/integrated Del: useless -- https://master.php.net/note/delete/116564/useless Del: bad code -- https://master.php.net/note/delete/116564/bad+code Del: spam -- https://master.php.net/note/delete/116564/spam Del: non-english -- https://master.php.net/note/delete/116564/non-english Del: in docs -- https://master.php.net/note/delete/116564/in+docs Del: other reasons-- https://master.php.net/note/delete/116564 Reject -- https://master.php.net/note/reject/116564 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201500) next »