Basic Syntax of PHP-microtime ()

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 number of microseconds. Format of the returned string: msec Sec

Syntax
microtime(get_as_float)
Parameters Description
Get_as_float If
The get_as_float parameter and its value is equivalent to true. This function returns a floating point number.
Description

This function only supports the gettimeofday () function "href =" http://www.w3school.com.cn/php/func_date_gettimeofday.asp ">
Gettimeofday () is available in the operating system called by the system.

If no optional parameter is required, this function uses "msec sec"
Returns a string, in which the SEC is from the Unix epoch (0:00:00 January 1, 1970
GMT) to the current number of seconds. 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 1221228252
1221228252.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 usually only one way to count the time consumed by executing an action, a function, or a 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
  1. <? PHP
  2. // Garvin: Measure query time.
  3. // Todo-item http://sourceforge.net/tracker/index.php? Func = detail & Aid = 571934 & group_id = 23067 & Atid = 377411
  4. $ Querytime_before = array_sum (explode ('', microtime ()));
  5. $ Result = @ pma_dbi_try_query ($ full_ SQL _query, null, pma_dbi_query_store );
  6. $ Querytime_after = array_sum (explode ('', microtime ()));
  7. $ Globals ['querytime'] = $ querytime_after-$ querytime_before;
  8. ?>

See article 2, array_sum () function usage. Here, array_sum (explode ('', microtime () is written in the PhP4 era. After PhP5, The microtime function has an additional bool value parameter, after adding this parameter, you can directly obtainArray_sum (explode ('', microtime (); 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

<?
$short_pi = "3.14159";
$my_pi = number_format($short_pi, 2);
echo $my_pi."/n";   // 3.14
$foo = 850017.9021;
$new_foo = number_format($foo, 3, ".", " ");
echo $new_foo."/n";  // 850 017.902
?>
Appendix 2 usage of array_num

Array_sum


(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 ()

$ 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) = 20
Sum (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.