How to use microtime() in PHP

phplogo.jpg

I was recently creating a simple quiz game and needed to record the time between a question being presented and an answer being given. Using the microtime() function in PHP was one simple way to track the time taken down to the microsecond.

The function is easy to use. The format is:

The output is as follows:

Now, combining a couple of variables and a little calculation will allow you to track the time taken.

Hopefully the code is self explanatory but feel free to ask questions in the comments.

Obviously, we are reliant on many external variables such as internet speed, local browser render speed, server etc but I think this provides a very simple method for calculating the time taken.

If you would like to learn more about PHP then enrol in the PHP for Beginners course.