By default, maximum execution time for PHP scripts is set to 30 seconds. If PHP scripts runs longer than 30 seconds, PHP stops the script and report an error. You can change max execution time using the max_execution_time directive in php.ini file.
PHP will exit with the following error once it reaches the maximum allowed time for it to execute your scripts;
Fatal error: Maximum execution time of 30 seconds exceeded in phpcript.php
You can fix this error by increasing the maximum execution time for php. This can be set globally or from within your PHP scripts from these options.
Increase Maximum Execution Time Limit of PHP Scripts
Follow the below methods to increase maximum execution time limit of php script.
Method #1 : Global PHP Configuration
This method will affect all the PHP scripts running in your system. Set the max_execution_time in your PHP configuration file (php.ini) to the number of seconds that want to allow your PHP scripts to execute.
In the below example I have set the maximum execution time for 2 minutes (120 Seconds).
; Maximum execution time of each script, in seconds ; http://php.net/max-execution-time ; Note: This directive is hardcoded to 0 for the CLI SAPI max_execution_time = 120
Setting it to 0 will impose no time limit whatsoever to the execution of your PHP scripts.
Restart your web server for the changes to take effect.
# /etc/init.d/httpd restart
Method #2 : ini_set()
This is basically the same as the previous solution, but it’s actually a function that you call from your PHP script. It’s a general function to override any configuration options set in your PHP’s configuration file and will only affect the execution of the scripts that call the function.
When placed at the start of your PHP script, the following function call will allow it to run for 120 seconds (5 minutes).
Method #3 : set_time_limit()
This is PHP’s built-in function specifically to set the maximum execution time limit of your PHP scripts. It’s to be called from your PHP script as in the previous method and the following example is to also set the limit to 120 seconds (2 minutes).
set_time_limit ( 300 )