In PHP, most time formats are represented by UNIX timestamps, and UNIX timestamps are units of measurement time in s (seconds) as the smallest unit. This is not accurate enough for some applications, so microtime() can be called to return the current UNIX timestamp and subtle number. The prototype of this function is as follows:
The code copy is as follows:
mixed microtime([bool get_as_float]); //Return the current UNIX timestamp and subtle number
An optional Boolean parameter can be provided for this function. If this parameter is not provided during call, this function returns a string in the format of "msec sec". where sec is the number of seconds from the UNIX epoch to the present, while msec is the subtle part, and both parts of the string are returned in seconds. If the get_as_float parameter is given and its value is equivalent to TRUE, microtime() returns a floating point number. The decimal point is still represented in timestamp format, while the decimal point is followed by a subtle value. But please note that the parameter get_as_float is newly added in PHP5.0 version, so in previous versions of PHP5, you cannot directly use this parameter to directly request a floating point number. In the following example, the microtime() function is called twice to calculate the time required to run the PHP script. The code looks like this:
The code copy is as follows:
<?php
//Life is a class that calculates the running time of a script
class Timer{
private $startTime = 0; //Save the time when the script starts executing (save in microseconds)
private $stopTime = 0; //Save the time when the script ends execution (save in microseconds)
//Call at the script start to get the microsecond value of the script start time
function start(){
$this->startTime = microtime(true); // Assign the obtained time to the member attribute $startTime
}
//Microsecond value of the end of the script using the script at the end of the script
function stop(){
$this->stopTime = microtime(true); // Assign the obtained time to the member attribute $stopTime
}
//Return the difference in the time obtained twice in the same script
function spent(){
//After calculation, 4 round 5, reserve 4 bits to return
return round(($this->stopTime-$this->startTime),4);
}
}
$timer= new Timer();
$timer->start(); //Call this method when the script file starts executing
usleep(1000); //The theme content of the script, here you can sleep for one millisecond as an example
$timer->stop(); //Call this method at the end of the script file
echo "Time to execute the script<b>".$timer->spent()."</b>";
?>
In the above script, declare a class Timer used to calculate the execution time of the script. You need to call the start() method in this class at the beginning of the script execution to get the time when the script starts executing. And call the stop() method in this class at the end of the script execution to get the time when the script is running. Then, by accessing the spent() method in this class, you can get the time required to run the script.