PHPackages                             ekinhbayar/interval-parser - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. ekinhbayar/interval-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

ekinhbayar/interval-parser
==========================

Interval Parser. Still a work in progress.

0.1.2(9y ago)62.0k3[4 issues](https://github.com/ekinhbayar/IntervalParser/issues)MITPHPPHP ^7

Since Sep 27Pushed 9y ago3 watchersCompare

[ Source](https://github.com/ekinhbayar/IntervalParser)[ Packagist](https://packagist.org/packages/ekinhbayar/interval-parser)[ Docs](https://github.com/ekinhbayar/IntervalParser)[ RSS](/packages/ekinhbayar-interval-parser/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (3)DependenciesVersions (5)Used By (0)

IntervalParser
==============

[](#intervalparser)

[![Build Status](https://camo.githubusercontent.com/5c177642a7d84c6470aad497ba7e94cd4b14931b125a3e4a37181724f1f99e55/68747470733a2f2f7472617669732d63692e6f72672f656b696e6862617961722f496e74657276616c5061727365722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ekinhbayar/IntervalParser)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ce1bd3068eb12eb216174a6a10a8ef51fa5c633321ce046e90dc470a33e4b733/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f656b696e6862617961722f496e74657276616c5061727365722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ekinhbayar/IntervalParser/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/535af74408a117121dc36c634353087041c21e21eda1daa307f08223f40bd565/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f656b696e6862617961722f496e74657276616c5061727365722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ekinhbayar/IntervalParser/?branch=master)

This is a work in progress.

---

This library provides three classes:

1. `IntervalFinder`, finds time intervals and any leading/trailing data, returns it as a TimeInterval object.

`IntervalFinder::find(string $input, int $flags) : TimeInterval`

2. `Parser`, takes a string and returns it as a DateInterval after passing it through the 3# method below.

`Parser::parse(string $input): \DateInterval`

3. `Normalizer`, finds and replaces non-strtotime-compatible abbreviations with compatible ones. It does not accept leading data, though it will return trailing data and already compatible abbreviations intact.

`Normalizer::normalize(string $input): string`

---

Allowed flags for `IntervalFinder::find` method are:

```
class IntervalFlags
{
    const INTERVAL_ONLY      = 0b00000000;
    const REQUIRE_TRAILING   = 0b00000001;
    const REQUIRE_LEADING    = 0b00000010;
    const MULTIPLE_INTERVALS = 0b00000100;
}

```

`IntervalFinder` takes a `ParserSettings` object that allows you to set which separators to use, defaults being:

```
string $leadingSeparationString = 'in',
bool $keepLeadingSeparator = false,
int $multipleSeparationType = self::SYMBOL,
string $multipleSeparationSymbol  = ',',
string $multipleSeparationWord = 'foo'

```

---

The `TimeInterval` is a value object that represents an interval of time. It is pretty much a DateInterval that holds extra information:

```
  int $intervalOffset
  int $intervalLength
  DateInterval $interval
  string $leadingData
  string $trailingData

```

---

**Installation**

`$ composer require ekinhbayar/interval-parser`

**Basic usage**

```
/**
 * Some other example inputs:
 *
 * foo in 7 weeks 8 days
 * 9 months 8 weeks 7 days 6 hours 5 minutes 2 seconds baz
 * remind me I have 10 minutes in 2 hours please
 *
 */
$trailing = '7mon6w5d4h3m2s bazinga!';
$leading  = 'foo in 9w8d7h6m5s';
$both = 'foo in 9d8h5m bar';
$onlyInterval = '9 mon 2 w 3 m 4 d';

# Set ParserSettings for IntervalFinder
$settings = new ParserSettings("in", false);
$intervalFinder = new IntervalFinder($settings, new Normalizer());

$intervalAndTrailing = $intervalFinder->find($trailing, IntervalFlags::REQUIRE_TRAILING);
var_dump($intervalAndTrailing);

$intervalAndLeading = $intervalFinder->find($leading, IntervalFlags::REQUIRE_LEADING);
var_dump($intervalAndLeading);

$intervalWithBoth = $intervalFinder->find($both, IntervalFlags::REQUIRE_TRAILING | IntervalFlags::REQUIRE_LEADING);
var_dump($timeIntervalWithBoth);

$intervalParser = new Parser(new Normalizer());

$dateInterval = $intervalParser->parse($onlyInterval);
var_dump($dateInterval);

# Multiple Intervals

# 1. Comma Separated
$multiple = 'foo in 9d8h5m bar , baz in 5 minutes, foo in 2 days 4 minutes boo, in 1 hr, 10 days';
$multipleIntervals = $intervalFinder->find($multiple, IntervalFlags::MULTIPLE_INTERVALS);
var_dump($multipleIntervals);

# 2. Separated by a defined-on-settings word

$settings = new ParserSettings("in", 1, ',', 'then');
$intervalFinder = new IntervalFinder($settings);

$wordSeparated = 'foo in 9d8h5m bar then baz in 5 minutes then foo in 2 days 4 minutes boo then in 1 hr then 10 days';
$wordSeparatedIntervals = $intervalFinder->find($wordSeparated, IntervalFlags::MULTIPLE_INTERVALS);
var_dump($wordSeparatedIntervals);
```

---

Thanks a ton to [PeeHaa](https://github.com/PeeHaa), [DaveRandom](https://github.com/DaveRandom), [bwoebi](https://github.com/bwoebi) and [pcrov](https://github.com/pcrov) for everything they made me learn and for all their help! :-)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance7

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 64.3% 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 ~93 days

Total

3

Last Release

3377d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6135846?v=4)[Ekin](/maintainers/ekinhbayar)[@ekinhbayar](https://github.com/ekinhbayar)

---

Top Contributors

[![PeeHaa](https://avatars.githubusercontent.com/u/1330296?v=4)](https://github.com/PeeHaa "PeeHaa (9 commits)")[![memclutter](https://avatars.githubusercontent.com/u/7972741?v=4)](https://github.com/memclutter "memclutter (3 commits)")[![ins0](https://avatars.githubusercontent.com/u/2622534?v=4)](https://github.com/ins0 "ins0 (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

parsertimeinterval

### Embed Badge

![Health badge](/badges/ekinhbayar-interval-parser/health.svg)

```
[![Health](https://phpackages.com/badges/ekinhbayar-interval-parser/health.svg)](https://phpackages.com/packages/ekinhbayar-interval-parser)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k936.5M2.3k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k942.7M149](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.1k155.2M841](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

3.0k426.1M944](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k260.4M293](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k204.2M72](/packages/sabberworm-php-css-parser)

PHPackages © 2026

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