Understanding PHP microtime() Function and Measuring Execution Time
This article explains how PHP's microtime() function returns the current Unix timestamp with microseconds, describes its optional parameter behavior, and provides a reusable example that measures script execution time using a custom microtime_float() helper.
The microtime() function returns the current Unix timestamp together with microseconds. When called without arguments it returns a string in the format "msec sec", where sec is the number of seconds since the Unix epoch and msec is the microsecond part.
If the optional boolean parameter $get_as_float is set to true , the function returns a float representing the full timestamp (seconds plus fractional microseconds).
Below is a practical example that defines a microtime_float() helper to obtain a float timestamp, measures the time taken by a short operation, and outputs the elapsed time.
The script records the start time, pauses execution for 100 microseconds with usleep() , records the end time, calculates the difference, and prints the elapsed time, demonstrating how to use microtime() for performance measurement.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.