Sunday, November 22, 2020

PHP | microtime() Function

PHP microtime() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP microtime() Function.

The PHP microtime() function returns the current Unix timestamp with microseconds. By default, this function returns a string value which contains microseconds and seconds which is separated by space (msec sec).

Syntax

Following below is the syntax to use this function -

microtime($get_as_float)


Parameter Details

Sr.NoParameter & Description
1

get_as_float(Optional)

This is a boolean value which is used to specify whether the result should be a floating point value or not. If you pass the boolean value true as a parameter, this function returns result as floating point value.


Return Value

This function returns the current Unix timestamp. By default this returns a string value in the form msec sec. If you pass the boolean value true as parameter to this function, then it returns the current time in seconds since Unix epoch which is accurate to the nearest microseconds.

PHP Version

This function was first introduced as part of the core PHP v 4 and, it works with all the later versions.

Example1

Following example illustrates the usage of microtime() function -

<?php
   $time = microtime();
   print($time);
?>

Output

When the above code is executed, it will produce the following result -

0.01174200 1606064533

Example2

Let us try to set the get_as_float value to true -

<?php
   $time = gettimeofday(true);    
   print_r($time); 
?>

Output

When the above code is executed, it will produce the following result -

1606064723.7694

Example3

Try the following example -

<?php
   $time_start = microtime(true);
   usleep(100);
   
   $time_end = microtime(true);
   $time = $time_end - $time_start;
   
   echo "Did nothing in $time seconds\n";
?>

Output

When the above code is executed, it will produce the following result -

Did nothing in 0.00095605850219727 seconds


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP mktime() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.