1. Home
  2. Technical
  3. PHP time limit (max_execution_time, set_time_limit())

PHP time limit (max_execution_time, set_time_limit())

The default PHP’s execution time (max_execution_time) is 30 seconds. This is the time limit for a PHP script for how many seconds the script can use 100% of CPU time.

It is possible to increase the time limit for a PHP script using the function set_time_limit() or by modifying the PHP configuration file php.ini or .user.ini.

Using the function set_time_limit() in a PHP script

Add the set_time_limit() function to the beginning of the required PHP file or before the code causing the problem with required number of seconds:

set_time_limit(60);

Using a global php.ini or directory-specific .user.ini file

max_execution_time = 60

By using a global php.ini file to increase the time limit will affect all virtual hosts of main and subdomains.
The local .user.ini file affects scripts located in the same directory and its subdirectories.

NB!

When increasing the time limit with an .ini file, caution must be taken to avoid negative effects if some other part of the application is slow or runs longer than 30 seconds, as this can lead to exceeding PHP processes limits with a higher number of visitors.

If a PHP script is taking more CPU time than the allowed limit, then similar error message will be generated:

PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /data01/virt1234/domeenid/www.example.com/htdocs/index.php on line 5
PHP documentation

The PHP time limit only affects the processing time of the PHP script itself. Time spent outside of this (such as database queries or system/server requests) is not counted toward this limit.
This also includes the sleep() function, where the time it takes does not count toward the time limit.

For more details, refer to the PHP documentation:
set_time_limit
max_execution_time

Help article for modifying the PHP configuration file

How to change php.ini file (max_execution_time, memory_limit)

Updated on 7. Nov 2024
Was this article helpful?

Related Articles