CODING ACADEMY

View Original

How to use microtime() in PHP

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:

See this content in the original post

The output is as follows:

See this content in the original post

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

See this content in the original post

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.