How to Start and Stop a Timer in PHP?

start and stop timers in PHP

How to Start and Stop a Timer in PHP? We can utilize the microtime() capacity to begin and stop the clock in PHP. The microtime() work noticed the time in microseconds when it’s called. We will ascertain the time distinction between the stop time and the beginning clock to get the execution time. The right grammar to utilize this capacity is as per the following.

I really want some data with respect to beginning and halting a clock in PHP. I want to gauge the slipped by time from the beginning of my .exe program (I’m utilizing executive() work in my php script) until it finishes the execution and show the time it required right away.

You can begin and stop a clock in PHP utilizing the microtime() work in PHP. The microtime() work is an inbuilt capacity in PHP which is utilized to return the current Unix timestamp with microseconds.

In this article, you will become familiar with the employments of the microtime() work.

Syntax:

microtime( $get_as_float )
  • Boundaries: The $get_as_float is sent as a boundary to the microtime() capacity, and it returns the string microsec as a matter of course.
  • Bring Value back: By default, the microtime() work returns the time as a string, yet on the off chance that it is set to TRUE, the capacity returns the time as a string. So the default is FALSE.

Example 1:

<?php
 
  // Displaying the micro time as a string
  echo ("Displaying the micro time as a string :");
  echo(microtime());
?>

Output

Displaying the micro time as a string :0.62248800 1620222618

Example 2:

<?php
 
    // Displaying the micro time as a float type
    echo ("Displaying the micro time as a float :");
    echo(microtime(true));
?>

Output

Displaying the micro time as a float :1620222618.9294

Note: The microtime()function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds.

Example 3: 

<?php
 
// Use microtime() function to measure
// starting time
$time_start = microtime(true);
 
// Code of program
$num = 0;
 
for( $i = 0; $i < 100000000; $i += 1 ) {
    $num += 5;
}
 
// Use microtime() function to measure
// ending time
$time_end = microtime(true);
 
// Time difference
$time = $time_end - $time_start;
 
echo "The speed of code = ".$time;
 
?>

Output

The speed of code = 3.625461101532

Example 4:

<?php
   //current time in milliseconds
    $milliseconds = round(microtime(true) * 1000);
    echo $milliseconds;
?>

Output

1620222618825

Also Read: Iterate over a List in Python: What is a List in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *