How to get the execution time of a MySQL query from PHP?

Today, We want to share with you check query execution time php.In this post we will show you php check time taken to execute script, hear for php get query execution time we will give you demo and example for implement.In this post, we will learn about PHP sleep delay Function increasing execution time with an example.

how to calculate query execution time in mysql?

  • MySQL Query the start time before the query is executed.
  • Execute your SQL query.
  • Query the end time after the query has finished running.
  • Calculate the difference in microseconds.
  • Format the time so that it only shows 10 decimal places.
  • Print out the seconds it took for the query to execute or run.


$started = microtime(true);

mysql_query("SELECT id, pname FROM products LIMIT 5");

$end = microtime(true);

$total_diff = $end - $started;

$getqueryTime = number_format($total_diff, 10);

echo "Here Simple SQL query took $getqueryTime seconds.";

example PHP code:

$sql_query = 'SELECT id, pname FROM products LIMIT 5';
$msc = microtime(true);
$total_diff = microtime(true)-$msc;
echo $total_diff . ' s'; // in seconds
echo ($total_diff * 1000) . ' ms'; // in millseconds

How to Measure Script Execution Time in PHP?

To measure the execution time of a PHP script, you can use the built-in functions in PHP to get the current time at the beginning and end of the script, and then calculate the difference between the two times. Here’s an example:

// Get the start time
$start_time = microtime(true);

// Your script code here...

// Get the end time
$end_time = microtime(true);

// Calculate the execution time
$execution_time = ($end_time - $start_time);

// Output the execution time
echo "Script executed in " . round($execution_time, 4) . " seconds.";

The microtime() function returns the current Unix timestamp with microseconds, and passing true as the function parameter returns a float value of the timestamp with microseconds.

Also Read This ๐Ÿ‘‰   jQuery Facebook Like Infinite Scroll Pagination in PHP MySQLi

The difference between the $end_time and $start_time gives you the execution time in seconds with microseconds. Finally, the round() function is used to round the execution time to four decimal places for readability.

How to Check Query Execution Time in PHP

To check the query execution time in PHP, you can use the microtime() function to get the current time in microseconds before and after the query execution and then calculate the difference. Here’s an example code snippet:

$start_time = microtime(true);

// Your database query goes here
$result = $db->query("SELECT * FROM my_table");

$end_time = microtime(true);

$execution_time = ($end_time - $start_time) * 1000; // Time in milliseconds

echo "Query executed in " . $execution_time . " ms";

In the code above, we use the microtime() function to get the current time in microseconds before and after the query execution. We calculate the difference between the two times, which gives us the query execution time in seconds. We then multiply the time difference by 1000 to convert it to milliseconds.

Finally, we output the execution time using echo. You can use this method to check the execution time of any database query in your PHP code.

I hope you get an idea about php calculate execution time.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.