Normally if the script timeout, the web server stops it and return an error while You have only a little chance of handling it by Yourself - by defining shutdown function.
But You could use a simple function of Your own, like this one:
function check_timeout($start) {
if(microtime() <= $start + MAX_EXECUTION_TIME)
return true;
return false;
}
while the MAX_EXECUTION_TIME
constant would be defined somewhere like
define('MAX_EXECUTION_TIME', 10000); // 10 seconds
Now somewhere in Your code You could do:
// some code...
$start = microtime();
foreach($array as $key => $value) {
if(check_timeout($start)) {
// do something
} else {
// set HTTP header, throw exception, etc.
// return false; // die; // exit;
}
}