PHP Microtime () Function Implementation Calculation page script run time instance detailed

Source: Internet
Author: User
Usually when we browse the site, often use the search function, in the search for information, careful users will find that at the bottom of the search results, generally have "search time for x seconds" of the word, which is the use of PHP The microtime () function.

The Microtime () function returns the current UNIX timestamp and the subtle number. Returns a string formatted as Mesc sec, where the SEC is the current Unix timestamp and msec is a subtle number.

The syntax format is as follows

Microtime (get_as_float);

Parameter description

When set to TRUE, the specified function should return a floating-point number, otherwise a string is returned. The default is FALSE.

Instance

Let's calculate the run time of a piece of code, first declaring a function run_time (), which returns the current time, precisely to the subtlety. Run the function one time before the PHP code snippet is run, and save the return value to the variable

$start _time, then run the PHP code snippet. Call the Run_time () function again when the code snippet is finished and save the return value to the variable $end_time, where the difference between the two variables is the time that the PHP snippet runs

The sample code is as follows:

<?phpheader ("Content-type:text/html;charset=utf-8");              Set the encoding/* Declaration run_time Variable */function run_time () {    list ($mesc, $sec) =explode ("", Microtime ());    return (float) $mesc + (float) $sec);} $start _time=run_time ();                                   First run code snippet/* Run code Snippet */$time 1=strtotime (Date ("y-m-d h:i:s"));                              Gets the current time $time2=strtotime ("2017-5-1");                              51 Holiday Time $time3=strtotime ("2017-8-15");                              Dragon Boat Festival Holiday Time $sub1=ceil (($time 2-$time 1)/3600);                            (60 seconds *60 minutes) seconds per hour $sub2=ceil (($time 3-$time 1)/86400);                           (60 seconds *60 minutes * 24 hours) seconds/day echo "51 off time and". " ". $sub 1." "." Hours!!! "echo" <p> "echo" from the Dragon Boat Festival holiday Time and "." ". $sub 2." "." Days!!! "; $end _time=run_time (); $time = $end _time-$start _time;//Run the code snippet again echo" <p> "; echo" The example runs at ". $time." Seconds ";? >

An example is detailed:

Explode function, the function is formatted as

Explode (Separator,string,limit)

The function is to cut the string (string) according to the specified string or character (separator), and if separator is empty (""), the function will return FALSE if the value contained in separator is not found in string. Then the function returns an array of single elements of string.

The list () function is formatted as:

List (Var1,var2 ...)

The function is to assign the values in the array to some variable var1.

Example run results such as:

Summarize

Through the preceding series of studies, we introduce the functions commonly used in PHP to deal with date and time, mainly divided into three aspects: the first is to introduce the system's time zone settings, and then introduced in PHP built-in date and time function, have obtained local timestamp mktime () function, get the current timestamp time () function The date and time date () function, gets the date information getdate () function, verifies the validity of the date checkdate () function, resolves dates and times to the Unix timestamp strtotime () function, Finally introduced the date and time the common method uses the Strtotime () function compares two time the size, uses the PHP time date function strtotime () realizes the Countdown function instance elaboration as well as explains in this chapter the realization computation page script running time and so on, hoped through this series of study, You can use the date and time functions in PHP skillfully.

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.