microtime

Measuring the time of PHP scripts - Using $_SERVER['REQUEST_TIME']

ぃ、小莉子 提交于 2019-12-02 02:27:49
问题 Are this methods a reliable way to measure a script: $time = ($_SERVER['REQUEST_TIME_FLOAT'] - $_SERVER['REQUEST_TIME']); or $time = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']); Which one should be used? And what's the difference of each one? They return very different measurements. 回答1: $time = ($_SERVER['REQUEST_TIME_FLOAT'] - $_SERVER['REQUEST_TIME']); This will never give you execution time of you PHP script. Because both the values are used for storing start of request . The

php time() and microtime() do sometimes not concord

◇◆丶佛笑我妖孽 提交于 2019-11-30 05:40:54
问题 While logging some data using microtime() (using PHP 5), I encountered some values that seemed slightly out of phase in respect to the timestamp of my log file, so I just tried to compare the output of time() and microtime() with a simple script (usleep is just here in order to limit the data output): <?php for($i = 0; $i < 500; $i++) { $microtime = microtime(); $time = time(); list($usec, $sec) = explode(" ", $microtime); if ((int)$sec > $time) { echo $time . ' : ' . $microtime . '<br>'; }

How to subtract microtime and display date with milliseconds in php?

别来无恙 提交于 2019-11-28 11:04:23
How to subtract microtime and display date with milliseconds in php ? For example: I have set end date and time $endtime = 2012-02-21 10:29:59; then I have current date or start date with converted from microtime $starttime = 2012-02-21 10:27:59.452; function getTimestamp() { $microtime = floatval(substr((string)microtime(), 1, 8)); $rounded = round($microtime, 3); return date("Y-m-d H:i:s") . substr((string)$rounded, 1, strlen($rounded)); } echo getTimestamp(); //sample output 2012-02-21 10:27:59.452 Now I want to subtract: $finaldate = $endtime - $starttime; I want my output to be like this:

Is using microtime() to generate password-reset tokens bad practice

泄露秘密 提交于 2019-11-28 04:03:51
问题 In PHP I've noticed some frameworks make use of the microtime() function to generate password reset tokens as in: $token = md5(microtime()); Is this a security issue? If the attacker is able to synchronize the clocks with the server to a degree of accuracy they could brute force the token. 1sec synchronization will only require 1,000,000 tries and this is not too crazy of an issue. How likely is this attack to succeed? Should one be generating tokens with /dev/urandom or openssl_pseudo_bytes(

php get microtime from date string

血红的双手。 提交于 2019-11-28 01:48:39
I am trying to get the time passed between two datetime strings (including milliseconds) example: $pageTime = strtotime("2012-04-23T16:08:14.9-05:00"); $rowTime = strtotime("2012-04-23T16:08:16.1-05:00"); $timePassed = $rowTime - $pageTime; echo $timePassed . "<br/><br/>"; What I want to see echoed is "1.2" but strtotime() ignores the millisecond part of the string. Also, apparently microtime() doesn't let you give it a datestring... Is there an alternative function for calculating this, or am I going to have to do some string parsing to extract the seconds and milliseconds and subtract? Try

Is a timestamp in microseconds always unique?

北城余情 提交于 2019-11-27 07:46:49
问题 uniqid() in PHP generates a unique ID based on the current timestamp in microseconds. Is that really a foolproof way to generate a unique ID? Even assuming there's a single user running a single script with a loop generating a timestamp in microseconds, can there still really be a theoretical guarantee that it's unqiue? And in practice, is the likelihood completely negligible? For clarity, say your loop is nothing more than this: foreach($things as $thing){ var_dump(microtime()); } is there

How to subtract microtime and display date with milliseconds in php?

纵然是瞬间 提交于 2019-11-27 06:00:19
问题 How to subtract microtime and display date with milliseconds in php ? For example: I have set end date and time $endtime = 2012-02-21 10:29:59; then I have current date or start date with converted from microtime $starttime = 2012-02-21 10:27:59.452; function getTimestamp() { $microtime = floatval(substr((string)microtime(), 1, 8)); $rounded = round($microtime, 3); return date("Y-m-d H:i:s") . substr((string)$rounded, 1, strlen($rounded)); } echo getTimestamp(); //sample output 2012-02-21 10

How to get current time with jQuery

≡放荡痞女 提交于 2019-11-27 04:59:31
问题 The following returns time in microseconds, for example 4565212462. alert( $.now() ); How do I convert it to a human readable time format, such as (hours:minutes:seconds) ? 回答1: You may try like this: new Date($.now()); Also using Javascript you can do like this: var dt = new Date(); var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds(); document.write(time); 回答2: You need to fetch all "numbers" manually like this: var currentdate = new Date(); var datetime = "Now: " +

php get microtime from date string

岁酱吖の 提交于 2019-11-26 22:03:02
问题 I am trying to get the time passed between two datetime strings (including milliseconds) example: $pageTime = strtotime("2012-04-23T16:08:14.9-05:00"); $rowTime = strtotime("2012-04-23T16:08:16.1-05:00"); $timePassed = $rowTime - $pageTime; echo $timePassed . "<br/><br/>"; What I want to see echoed is "1.2" but strtotime() ignores the millisecond part of the string. Also, apparently microtime() doesn't let you give it a datestring... Is there an alternative function for calculating this, or

How to benchmark efficiency of PHP script

怎甘沉沦 提交于 2019-11-26 05:48:09
I want to know what is the best way to benchmark my PHP scripts. Does not matter if a cron job, or webpage or web service. I know i can use microtime but is it really giving me the real time of a PHP script? I want to test and benchmark different functions in PHP that do the same thing. For example, preg_match vs strpos or domdocument vs preg_match or preg_replace vs str_replace` Example of a webpage: <?php // login.php $start_time = microtime(TRUE); session_start(); // do all my logic etc... $end_time = microtime(TRUE); echo $end_time - $start_time; This will output: 0.0146126717 (varies all