PHPackages                             morjodrom/time-limiter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. morjodrom/time-limiter

ActiveLibrary[HTTP &amp; Networking](/categories/http)

morjodrom/time-limiter
======================

A class helping to track time left to perform a php script

2.0.0(1y ago)010[1 PRs](https://github.com/Morjodrom/time-limiter/pulls)MITPHPPHP &gt;=7.0CI passing

Since Jan 12Pushed 3mo agoCompare

[ Source](https://github.com/Morjodrom/time-limiter)[ Packagist](https://packagist.org/packages/morjodrom/time-limiter)[ Docs](https://github.com/Morjodrom/time-limiter)[ RSS](/packages/morjodrom-time-limiter/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Time Limiter
============

[](#time-limiter)

A small utility class to help to limit script execution time.

Basic usage
===========

[](#basic-usage)

The class is usually used within loops with heavy time consumption.

```
// maximum time to execute one request
$REQUEST_TIMEOUT_SEC = 5;

$curlClient = new SomeCurlClient(); // just an example.

// shared hosting often has a limit. E.g. 30 seconds
$maxExecutionTime = ini_get('max_execution_time');
$timeLimiter = new \timelimiter\TimeLimiter($maxExecutionTime, $REQUEST_TIMEOUT_SEC);

// check if there is time left to prevent 504 timeout
// recommended
foreach($timeLimiter => $timeLeft){
  $result = $curlClient->doSomeHeavyJob([
      'timeout' => $REQUEST_TIMEOUT_SEC
  ]);
  // handle the result
  // ...
}

// or alternatively while loop might be used with respective Iterator calls.
while($timeLimiter->valid()){
  $result = $curlClient->doSomeHeavyJob([
      'timeout' => $REQUEST_TIMEOUT_SEC
  ]);
  $timeLimiter->next(); // must be called to adapt to long iterations
}
```

Installation
============

[](#installation)

As a production dependency using [Composer](https://getcomposer.org/):

```
composer require morjodrom/time-limiter

```

As a dev-dependency for deveopment-only cases:

```
composer require --dev morjodrom/time-limiter

```

Constructor options
===================

[](#constructor-options)

`int $limitSeconds` - seconds to process. 0 equals to no limit. Value from `ini_get('max_execution_time')` might be the desired option.

`[int $preliminaryTimeout] = DEFAULT_TIME_UP_SECONDS = 3` seconds to stop execution preliminary before reaching the timeout. `$preliminaryTimeout` must be a bit greater that the longest *theoretical* operation you perform in a loop. Therefore, the last risky operation that might exceed the execution time is omitted. The class tracks the time spend on each iteration to update $preliminaryTimeout to equal the longest operation

- `[int|null $startTimestamp = $_SERVER['REQUEST_TIME']` is used by default. Must be a timestamp since Unix Epoch (January 1 1970 00:00:00 GMT), e.g. `time()` call.

It is highly encouraged to use `foreach` construction. Raw `while` iteration also is possible with correct Iterator calls

Methods
=======

[](#methods)

`current(): int` - returns number of seconds left before the timeout `valid(): bool` - returns if there is time left to perform the script safely `next(): void` - must be called after a finished iteration to adapt to unexpected long iterations

Support
=======

[](#support)

Feel free to open an issue:

License - MIT
=============

[](#license---mit)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance61

Regular maintenance activity

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2412 days

Total

2

Last Release

628d ago

Major Versions

1.0.0 → 2.0.02024-08-20

PHP version history (2 changes)1.0.0PHP &gt;=5.3

2.0.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/be81e7e9622ef16127f9870ae03b19ff26291b08d5f790b3ee20619df6d7ec9f?d=identicon)[Morjodrom](/maintainers/Morjodrom)

---

Top Contributors

[![Morjodrom](https://avatars.githubusercontent.com/u/11189913?v=4)](https://github.com/Morjodrom "Morjodrom (19 commits)")

---

Tags

curlexecution-timelooptimeouttimeoutlimitmax\_execution\_time

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/morjodrom-time-limiter/health.svg)

```
[![Health](https://phpackages.com/badges/morjodrom-time-limiter/health.svg)](https://phpackages.com/packages/morjodrom-time-limiter)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M318](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48247.0M383](/packages/php-http-curl-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
