PHP time limit – extend script running time

December 7th, 2011 | By: | PHP | No comments »

Some scripts require some time to execute, and they might reach your PHP time limit.

Fatal error

If PHP time limit is too short for your script, you will see this error:

Fatal error: Maximum execution time of N seconds exceeded in /path/to/script.php on line N

Configuring php.ini

PHP time limit can be configured in php.ini, open it up and change the following line to something more suitable:

(EDIT: Can’t find it? Look where is php.ini)

max_execution_time = 30

NOTE: PHP time limit is measured in seconds!

In order to enable PHP time limit, Apache needs to be restarted after php.ini has been edited.

Setting PHP time limit within a script

If you can’t edit your php.ini, or you want to set a time limit only for one script, you can set it with set_time_limit(N) function, which sets the time limit in seconds.

This will set to no time limit:

set_time_limit(0);

And this line to one minute:

set_time_limit(60);

NOTE: When dealing with resource-consuming operations, PHP might exceed the memory limit! However, other than PHP time limit, you can also set the PHP memory limit.