downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003


(PHP 3, PHP 4 )

set_time_limit -- Limits the maximum execution time


void set_time_limit ( int seconds)

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. If seconds is set to zero, no time limit is imposed.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.


set_time_limit() has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.

add a note add a note User Contributed Notes
php at
27-Jun-2003 03:30
You may also need to look at Apache's timeout setting (Win32 version for me), I changed max execution time value in php.ini, and still got stopped by Apache's timeout value in the httpd.conf file.
rsallo at gna dot NOSPAM dot es
30-May-2003 06:28
When you are working with IIS, PHP timeout is valid only when it's lower than script timeout defined by IIS.

IIS 5 has a default timeout of 300 seconds. If you need a higher timeout, you also have to change IIS properties. Otherwise, your server will stop your PHP script before it reaches its own timeout.
23-Mar-2003 10:56
I think it might be useful to point out that set_time_limit() doesn't have any effect on stream operations. A stalled script, either during open operations (e.g. fsockopen()) or read operations (e.g. fgets()), can make your script run "forever".

So it would be advisable to define timeouts for opening sockets (with the timeout parameter in fsockopen()) and for reading data from sockets (with socket_set_timeout()). Rudenko Ilya
16-Mar-2003 10:50
So if you use dynamic content, size of which always diffrent
and you didn't know how long your script should execute. But you
can calculate duration of one while iteration. Use set_time_limit()
inside while directive. So if script work is normal you never get
"maximum execution time error", but if it have error inside while
control structure it will be breaked with error.

WHILE($list = mysql_fetch_assoc($result) {
    set_time_limit(2);    // Timelimit 2 seconds for sending each mail
    mail($list[email], $list[Subject], $list[Message]);
set_time_limit(5);    // Timelimit to finish script
ingo dot land at sapito dot nl
23-Dec-2002 09:20
The load time of very long PHP scripts (I have for a script of 8 MB generated for data migration) can generate a time out before the set_time_limit function is evaluated.

Example 1 results in a time-out at the first line of the script:

//20 minutes time limit
//now the actual script starts
statement one;
statement two; ...

Example 2 works fine:

//20 minutes time limit
//data_migration.php contains statement one, statement two, ...
require 'data_migration.php.'
usere at example dot com
12-Dec-2002 05:02
edit your php.ini file


; Resource Limits ;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
memory_limit = 8M      ; Maximum amount of memory a script may consume (8MB)

just set the max_execution_time to what ever you need it to be in seconds
opaf at yahoo dot com
07-Jun-2002 11:47
It seems that set_time_limit does take into account the time spent in functions like sleep:

echo "<html>\n" ;
"<body>\n" ;
"Time limit: 10 seconds<br/>" ;
"Timer: <span id=\"counter\"></span><br/>\n" ;
"</html>" ;

set_time_limit 10 ) ;
$w ;
  while ( 
true ) {
$w ++ ;
flush () ;
sleep ) ;
"<script>document.all.counter.innerHTML = \"$w\";</script>\n";

The above script will never reach 10 seconds.
16-Oct-2001 07:04
user abort stop the script
see "ignore_user_abort"

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST