How to Resolve the Fatal error: Maximum Execution time of 30 seconds exceeded in PHP
In this tutorial, we will show you handy solutions to implement once you encounter the fatal error: maximum execution time of 30 seconds exceeded in PHP.
Encountering the fatal error Maximum execution time of 30 seconds exceeded in PHP means a script ran longer than the allowed limit set in the PHP configuration. To resolve this, you can adjust the time limit using the set_time_limit() function or modify the php.ini file. Below are two solutions.
Solution A
Follow these steps to update the configuration file:
Step 1
Locate and open your php.ini file. You can find its path by running php --ini in the terminal or checking phpinfo().
Step 2
Find the max_execution_time directive and change its value to a higher number (e.g., max_execution_time = 300).
Step 3
Restart your web server or PHP-FPM service for the changes to take effect.
Solution B
Now, let’s see an alternative solution. This option is often more practical for specific scripts.
Add the set_time_limit() function to your PHP code to override the default limit at runtime:
set_time_limit(600); // Sets the maximum execution time to 600 secondsThe input parameter specifies the maximum execution time in seconds. Passing 0 sets an unlimited execution time. Note that set_time_limit() only affects script execution time and may not work in all server environments (e.g., some PHP-FPM or Apache configurations ignore it). Avoid setting excessively high values to prevent server hangs.