PHP microtime function

 The PHP microtime function can be used to read the current Unix timestamp of the server and the number of microseconds. The time or mktime function of PHP can only return to seconds , while the microtime function can return to milliseconds below the decimal point. For example, time can return 1415335769. Microtime can return 1415335769.36 such a Unix timestamp. In other words, microtime is more accurate than the time function, but microtime can only be used in an operating system environment that supports the gettimeofday() system call.


Basic syntax of PHP microtime function
microtime(get_as_float);
PHP microtime originally has no parameters to use. The returned value is composed of two numbers, expressed like "msec sec", where msec is the position of microseconds , and sec is seconds . The units of both are actually seconds . The actual appearance is like "0.35679800 1415335769", where 0.35679800 is in seconds, and 1415335769 is also in seconds.

In the traditional case, to obtain a result like 1415335769.37, it must be processed by other functions such as explode. It is really a bit troublesome, until the PHP 5.4.0 version began to add the get_as_float parameter, if the get_as_float parameter is set to true, microtime will return a The Unix timestamp with two decimal places can easily get the result we want, that is, directly output the result "1415335769.37". The actual operation of this part will be clearly presented to you in the example.

PHP microtime function application example
<?php
  echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; //Web page encoding declaration
  echo'Current Unix timestamp seconds:'. time( ).'Seconds<br>';
  echo'Result of PHP microtime:'.microtime().'<br>';
  echo'Current Unix timestamp seconds:'.microtime(true).'second';
?>
Example output results (for illustration only)
Current Unix timestamp seconds: 1415337041 seconds
PHP microtime result: 0.01306500 1415337041
Current Unix timestamp seconds: 1415337041.01 seconds
The sample Unix timestamp output is three times. The first time is to directly read the Unix timestamp of the server with a simple time function, and the second output is to directly output the read result of the microtime function. 0.01306500 and 1415337041 are both Unix The second of the timestamp, the difference is that the previous 0.01306500 is a decimal point, usually called microseconds, the third output is to first set the microtime parameter get_as_float to true, so that you can get a result like 1415337041.01.

Supplement, let microtime use the true parameter to output the result with rounding function.

Post a Comment

0 Comments