Get max_execution_time in PHP script

Get max_execution_time in PHP script

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving max_execution_time in PHP Scripts

When you configure execution limits, you usually start by setting them rather than reading them. Standard practice involves calling ini_set() to modify the directive within a script or using set_time_limit() to override it for specific blocks of code. However, there is a distinct scenario where you need to inspect the current state before making changes. Perhaps you are building an admin interface that displays server configuration, or you are debugging why a long-running task is terminating prematurely. In these cases, you need a variable containing the maximum execution time in seconds.

The most reliable way to access this value is by reading the max_execution_time directive directly from the PHP runtime environment. You can achieve this using the ini_get() function. This function retrieves the current value of a specified configuration option. When passed the string 'max_execution_time', it returns the limit as an integer representing seconds.

Here is how you implement it:

$limit = ini_get('max_execution_time');

// Output or use $limit
echo "Current max execution time: {$limit} seconds";

It is important to understand what value ini_get() actually returns in this context. If the limit is set to -1, it signifies that there is no maximum execution time; the script will run until it finishes or hits a system-level timeout (often 60 minutes on Apache). When you store this result in a variable, you must account for both integer seconds and the sentinel value of -1.

$limit = ini_get('max_execution_time');

if ($limit === -1) {
    echo "No time limit is set.";
} else {
    echo "Time limit is set to {$limit} seconds.";
}

You might wonder if set_time_limit() or ini_set() offers a way to read the value back. While these functions modify the directive, they do not return the new value upon successful execution. Furthermore, set_time_limit() does not accept a negative value; passing -1 will actually set the limit to 30 seconds (the default), rather than disabling the limit entirely. This is a critical distinction when interacting with the runtime limits programmatically versus using ini_get().

In web development frameworks like Laravel, checking this value becomes even more relevant when handling long-running tasks such as large file uploads or complex data processing jobs. If you encounter a Fatal error: Allowed memory size exceeded or a script termination that seems abrupt, verifying the execution time limit is a standard diagnostic step. You can create a simple diagnostic route to inspect these settings before your application logic runs.

// Inside a Laravel controller or route
return response()->view('diagnostics', [
    'max_execution_time' => ini_get('max_execution_time'),
    'memory_limit'       => ini_get('memory_limit'),
]);

There are also edge cases involving CLI versus web environments. In command-line scripts, ini_get() behaves consistently with the php.ini configuration or values set via set_time_limit(). However, when running under a web server like Apache or Nginx with PHP-FPM, the process manager might enforce its own timeouts that are independent of the PHP script's internal limit. Even if you retrieve a value of 30 seconds via ini_get(), the request might still die earlier due to an Apache ProxyTimeout or similar server-level settings. Always treat the value returned by ini_get() as the PHP-specific floor, not necessarily the absolute ceiling for your application's lifecycle.

If you need to dynamically adjust this limit based on the current configuration, reading it first ensures you aren't accidentally setting a lower limit than what was previously configured. For instance:

$current = ini_get('max_execution_time');
$newLimit = $current + 60; // Add 60 seconds to the current limit

ini_set('max_execution_time', $newLimit);

This approach preserves the existing configuration intent while applying a specific adjustment, ensuring predictable behavior across different server deployments.

Our network