


Detailed explanation of the php microtime() function to calculate the running time of page scripts
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);
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."秒"; ?>
Detailed explanation of the example:
explode function, the function format is
explode(separator,string,limit)
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...)
The function of this function is to assign the values in the array to some variables var1.
The sample running results are as follows:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
