Microtime () function in PHP

Source: Internet
Author: User
Tags float number

First, we will introduce the usage of this function:

Definition and usage (PhP5)
The microtime () function returns the current UNIX Timestamp and the number of microseconds. Format of the returned string: msec Sec

Syntax
Microtime (get_as_float)
Parameter description
If the get_as_float parameter is given and its value is equivalent to true, this function returns a floating point number.

Description
This function is only available in the operating system that supports the gettimeofday () System Call.

If no optional parameter is required, this function returns a string in the format of "msec sec", where sec is the current number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT, MSEC is in microseconds. The two parts of the string are returned in seconds.

Code:
<? PHP
Echo microtime (); echo '<br/>'; echo microtime (true );
?>
Browser display result:
0.45316500 12212282521221228252.45
We can see that microtime (true) returns only two decimal places. If you want to output more microseconds, you need to use the number_format () function. See the usage of the function attached with 1.
Supplement: the usage of microtime () in PhP4 does not include the true parameter. To obtain a floating point number, use the array_num () and explode () functions together.
For example:
In PHP, there is always only one way to count the time consumed by the execution of an action, function, or process: record the timestamp before the operation, after calculation, record the timestamp and subtract it to get a relatively practical time. The basic code is as follows (I copied it from phpMyAdmin, so I am too lazy to call it. This code is actually the calculation of the SQL Execution time in phpMyAdmin ):
PHP code
<? PHP
// Garvin: Measure query time.
// Todo-item http://sourceforge.net/tracker/index.php? Func = detail & Aid = 571934 & group_id = 23067 & Atid = 377411
$ Querytime_before = array_sum (explode ('', microtime ()));

$ Result = @ pma_dbi_try_query ($ full_ SQL _query, null, pma_dbi_query_store );

$ Querytime_after = array_sum (explode ('', microtime ()));

$ Globals ['querytime'] = $ querytime_after-$ querytime_before;

?>

ViewArticleThe last part is the usage of the array_sum () function. Here, array_sum (explode ('', microtime () is written in the PhP4 era. After PhP5, The microtime function has an additional bool value parameter, after this parameter is added, array_sum (explode ('', microtime () can be obtained directly. Equal Value: microtime (true ). Appendix 1: number_format () Usage
Number_format
Format the numeric string. Syntax: String number_format (float number, int [decimals], string [dec_point], string [thousands_sep]); Return Value: String correspondence type: mathematical operation
 
Description

This function is used to format the floating point parameter number.
If the decimals parameter is not added, only the integer part of the returned string is added. This parameter is returned based on the number of decimal places specified by the parameter. The dec_point parameter indicates the decimal point representation method. The specified value is ".". You can change it to another decimal point. The thousands_sep parameter is the separator number of each three digits in the integer part. The specified value is ",".
The most special part of this function is the number of parameters. There must be at least one, that is, the string to be formatted. There can also be two or four parameters, but not three parameters. It is worth noting that the number after the specified decimal point is discarded without rounding.
 
Example


Appendix 2 usage of array_num
array_sum
usage
(PHP 4> = 4.0.4) array_sum -- calculate the sum of all values in the array. Description: Mixed array_sum (array) array_sum () returns the sum of all values in the array as an integer or floating point number. Example 1. array_sum () example $ A = array (2, 4, 6, 8); echo "sum (A) = ". array_sum ($ ). "/N"; $ B = array ("A" => 1.2, "B" => 2.3, "C" => 3.4); echo "sum (B) = ". array_sum ($ B ). "/N"; the above Program output is: sum (A) = 20sum (B) = 6.9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.