A PHP “Maximum execution time exceeded” message identifies where PHP stopped the request, not necessarily the operation that consumed the time. A harmless call such as time() may be the next line PHP reaches after an earlier query, network call, file operation, or loop used the available budget.
Last updated: September 29, 2026.
<?php
function timed(string $label, callable $work): mixed
{
$started = hrtime(true);
try {
return $work();
} finally {
$seconds = (hrtime(true) - $started) / 1_000_000_000;
error_log(sprintf('%s took %.3f seconds', $label, $seconds));
}
}
$rows = timed('load report rows', fn () => loadReportRows());Measure meaningful sections around the suspected work instead of instrumenting only the line named in the fatal error. Use production-safe logging and never include passwords, tokens, or sensitive row data.
Confirm the effective limit
Log ini_get('max_execution_time') from the same web runtime that fails. Command-line PHP commonly has different configuration from PHP-FPM or Apache. The PHP configuration reference also notes that measured time differs by platform: on non-Windows systems, time spent in some system calls and database or stream operations is not counted the same way as on Windows.
Fix the slow operation
Use the timings to narrow the request. Add indexes for demonstrably slow queries, place explicit timeouts on HTTP and socket calls, stream large files instead of loading them at once, and add a clear exit condition to loops. The guides to reading large CSV files and streaming large JSON documents show bounded-memory patterns that also make long jobs easier to observe.
For interactive requests, move large imports, exports, and report generation to a queue or background worker. Save progress in restartable batches so one failure does not discard the entire job.
Increase the limit only deliberately
set_time_limit() restarts PHP’s execution timer, but it is not a repair for an unbounded query or loop. Raise the limit only when the operation is expected, measured, and protected by its own database, network, and job timeouts. For step-through diagnosis, use Xdebug; for uploads, keep request size and processing bounded as described in the secure upload pipeline.