Detailed explanation of the php microtime() function to calculate the running time of page scripts

怪我咯
Release: 2023-03-07 14:44:01
Original
3205 people have browsed it

Usually when we browse the website, we often use the search function. When searching for information, careful users will find that at the bottom of the search results, there is usually the words "search time is x seconds". This is using PHP's microtime() function.

The microtime() function returns the current UNIX timestamp and microseconds. Returns a string in the format mesc sec, where sec is the current UNIX timestamp and msec is the number of microseconds.

The syntax is as follows

microtime(get_as_float);
Copy after login

Parameter description

When set to TRUE, it specifies that the function should return a floating point number, otherwise it returns a string. Default is FALSE.

Example

Let's calculate the running time of a piece of code. First, declare a function run_time(), which returns the current time, accurate to the microsecond. Run the function once before running the PHP code segment, save the return value to the variable

$start_time, and then run the PHP code segment. When the code segment is finished running, call the run_time() function again and save the return value to the variable $end_time. The difference between the two variables is the running time of the PHP code segment.

The sample code is as follows:

<?php
header("Content-type:text/html;charset=utf-8");              //设置编码

/*声明run_time变量*/
function run_time(){
    list($mesc,$sec)=explode(" ",microtime());
    return ((float)$mesc+(float)$sec);
}
$start_time=run_time();                                   //第一次运行代码段

/*运行代码段*/
$time1=strtotime(date("Y-m-d H:i:s"));                              //获取当前时间
$time2=strtotime("2017-5-1");                              //五一放假时间
$time3=strtotime("2017-8-15");                              //端午放假时间

$sub1=ceil(($time2-$time1)/3600);                            //(60秒*60分)秒/小时
$sub2=ceil(($time3-$time1)/86400);                           //(60秒*60分*24小时)秒/天

echo "离五一放假时间还有"." ".$sub1." "."小时!!!";
echo "<p>";
echo "离端午节放假时间还有"." ".$sub2." "."天!!!";

$end_time=run_time();

$time=$end_time-$start_time;
//再次运行代码段
echo "<p>";

echo "该示例的运行时间为".$time."秒";

?>
Copy after login

Detailed explanation of the example:

explode function, the function format is

explode(separator,string,limit)
Copy after login

The function of this function is to convert the string according to the specified String or character (separator) cut, if the separator is empty (""), then the function will return false; if the value contained in the separator is not found in the string, then the function will return an array of single elements of the string .

list() function, the function format is:

list(var1,var2...)
Copy after login

The function of this function is to assign the values ​​in the array to some variables var1.

The sample running results are as follows:

Detailed explanation of the php microtime() function to calculate the running time of page scripts

Summary

Through the previous series of studies, We introduce the commonly used functions for processing date and time in PHP, which are mainly divided into three aspects: first, it introduces the system time zone settings, and then introduces the built-in date and time functions in PHP. There are Local timestamp mktime() function , Get the current timestamp time() function , Get the current date and time date() function , Get date information getdate() function , Check the validity of the date checkdate() function , Parse the date and time into a UNIX timestamp strtotime() function , and finally introduce the date and time Commonly used methodsUse the strtotime() function to compare the size of two times,Use the PHP time and date function strtotime() to implement the countdown function detailed exampleAnd the calculation page script explained in this chapter The running time, etc. I hope that through this series of studies, everyone can skillfully use the date and time functions in PHP.

The above is the detailed content of Detailed explanation of the php microtime() function to calculate the running time of page scripts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template