PHPackages                             wapmorgan/time-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. wapmorgan/time-parser

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

wapmorgan/time-parser
=====================

A parser for date &amp; time written in natural language

2.0.0(9y ago)97127.1k↓17.4%23MITPHPPHP &gt;=5.6.0

Since Nov 7Pushed 7y ago11 watchersCompare

[ Source](https://github.com/wapmorgan/TimeParser)[ Packagist](https://packagist.org/packages/wapmorgan/time-parser)[ RSS](/packages/wapmorgan-time-parser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

TimeParser
==========

[](#timeparser)

A parser for date and time written in natural language for PHP.

[![Composer package](https://camo.githubusercontent.com/400705e32ffdf7106a165a5eaed9ca32b217aa0e52d3527269b10729a01fd80e/687474703a2f2f636f6d706f7365722e6e6574776f726b2f62616467652f7761706d6f7267616e2f74696d652d706172736572)](https://packagist.org/packages/wapmorgan/time-parser) [![Latest Stable Version](https://camo.githubusercontent.com/1ba1c8f23c0374af5fc57166108e2d00480665728a2c561c13bf4581e25fa5d5/68747470733a2f2f706f7365722e707567782e6f72672f7761706d6f7267616e2f74696d652d7061727365722f762f737461626c65)](https://packagist.org/packages/wapmorgan/time-parser) [![Total Downloads](https://camo.githubusercontent.com/1d12c585a2a98955b67ea241e93e66f06e3cf087320ad62c3bc9b36b81803485/68747470733a2f2f706f7365722e707567782e6f72672f7761706d6f7267616e2f74696d652d7061727365722f646f776e6c6f616473)](https://packagist.org/packages/wapmorgan/time-parser) [![Latest Unstable Version](https://camo.githubusercontent.com/0ba957fa920818ea47633db1483405fad36180003e8cbdccd6dd16906d4dccb2/68747470733a2f2f706f7365722e707567782e6f72672f7761706d6f7267616e2f74696d652d7061727365722f762f756e737461626c65)](https://packagist.org/packages/wapmorgan/time-parser) [![License](https://camo.githubusercontent.com/911cd8962edcabe94a280f91dbf23725f2caf012d70754003ac6c7163ef5db76/68747470733a2f2f706f7365722e707567782e6f72672f7761706d6f7267616e2f74696d652d7061727365722f6c6963656e7365)](https://packagist.org/packages/wapmorgan/time-parser) [![Testing](https://camo.githubusercontent.com/2feace2038a021e241c82fb3c6992f36e937f8529d2e6e84a820f01b65d24e01/68747470733a2f2f7472617669732d63692e6f72672f7761706d6f7267616e2f54696d655061727365722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/wapmorgan/TimeParser)

1. Installation
2. Usage
3. Languages support
4. ToDo

Installation
------------

[](#installation)

The preferred way to install package is via composer:

```
composer require wapmorgan/time-parser
```

Usage
-----

[](#usage)

Parse some input from user and receive a `DateTime` object.

1. Create a Parser object

    ```
    $parser = new wapmorgan\TimeParser\TimeParser('all');
    ```

    First argument is a language. Applicable values:

    - `'all'` (by default) - scan for all available languages. Use it when you can not predict user's preferred language.
    - `'russian'` - scan only as string written in one language.
    - `array('english', 'russian')` - scan as english and then the rest as russian.
2. Enable and disable parsing of alphabetic values.

    ```
    // To enable alphabetic parsing.
    $parser->allowAlphabeticUnits();
    // To disable alphabetic parsing.
    $parser->disallowAlphabeticUnits();
    ```
3. Parse string and return a `DateTimeImmutable` object. If second argument is `true`, method will return `false` when no date&amp;time strings found. If third parameter is provided, then it is filled with the string obtained after all the transformations.

    ```
    $datetime = $parser->parse(fgets(STDIN));
    // next call returns false
    $datetime = $parser->parse('abc', true);
    // $result will contains "we will come "
    $datetime = $parser->parse('We will come in a week', true, $result);
    ```
4. For advanced parsing of alphabetic values is used built-in function. You can specify your own handler for this feature. Сurrently is used for russian and english languages only.

    ```
    use wapmorgan\TimeParser\TimeParser;

    // $string will contains alphabetic value for advanced parsing.
    // Ex.: for string "in twenty five minutes", it will contains "twenty five".
    // $lang will contains name of the parsed language.
    TimeParser::setWordsToNumberCallback(function($string, $lang) {
        // do some magic
    });

    ```
5. You can add your own custom language.

    ```
    // $date must be an array with a structure like the existing rules.
    $parser->addLanguage($data);
    ```

Languages support
-----------------

[](#languages-support)

For this moment four languages supported: Russian, English, French and German. Two languages support is in progress: Chinese, Spanish. Their rules are in `rules` catalog so you can improve TimeParser by adding new language or by improving existing one.

Languages with examples of strings containing date&amp;time:

LanguageExamplechinese15:12:13 下星期一 下年 二月 経由15小时 経由10分钟 経由11秒钟 経由5年 経由2星期 経由1天 経由10月english15 december 1977 at 15:12:13 next monday next year in february in 15 hours in 10 minutes in 11 seconds in 5 years in 2 weeks in 1 day in 10 monthsfrench15:12:13 prochaine lundi prochaine année février bout de 15 heures bout de 10 minutes bout de 11 secondes bout de 5 années bout de 2 semaines bout de 1 jour bout de 10 moisgerman15:12:13 nächsten montag nächsten jahr im februar nach 15 uhr nach 10 minuten nach 11 secunden nach 5 jahre nach 2 wochen nach 1 tag nach 10 monaterussian15 декабря 1977 года в 15:12:13 в следующий понедельник в следующем году в феврале через 15 часов через 10 минут через 11 секунд через 5 лет через 2 недели через 1 день через 10 месяцевspanish15:12:13 el próximo lunes en próximo año en febrero en 15 horas en 10 minutos en 11 segundos en 5 años en 2 semanas en 1 día en 10 mesesFor developing reasons you may would like to see process of parsing. To do this call related methods:

```
TimeParser::enableDebug();
// and
TimeParser::disableDebug();
```

### Parsable substrings

[](#parsable-substrings)

To understand, how it works, look at substrings separately:

- **15 december 1977** - absolute date
- **at 15:12:13** - absolute time
- **next monday** or **this friday** - absolute date
- **next year** or **2016 year** - absolute date
- **in february** or **next month** - absolute date
- **next week** - absolute date
- **in 15 hours** - relative time
- **in 10 minutes** - relative time
- **in 11 seconds** - relative time
- **in 2 weeks** - relative date
- **in 1 day** - relative date
- **in 10 months** - relative date
- **через час** - relative date (russian, english only)
- **in a hour** - relative date (russian, english only)
- **через двадцать пять минут** - relative date (russian, english only)
- **in twenty five minutes** - relative date (russian, english only)
- **пять минут назад** - relative date (russian, english only)
- **five minutes ago** - relative date (russian, english only)
- **5 минут назад** - relative date (russian, english only)
- **5 minutes ago** - relative date (russian, english only)
- **вчера** - relative date (russian, english only)
- **yesterday** - relative date (russian, english only)

ToDo
----

[](#todo)

- Tests.
- Try to parse combinations: *in 5 hours and 2 minutes*.
- Try to parse alphabetic offsets: *in five hours* and *через пять часов*.

### Languages ToDo

[](#languages-todo)

- Chinese - check hieroglyphs.
- Spanish - check prepositions.
- Portuguese
- Arabic
- Korean

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity48

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 78.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 ~178 days

Total

4

Last Release

3308d ago

Major Versions

1.0.2 → 2.0.02017-04-27

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6000618?v=4)[Sergey](/maintainers/wapmorgan)[@wapmorgan](https://github.com/wapmorgan)

---

Top Contributors

[![wapmorgan](https://avatars.githubusercontent.com/u/6000618?v=4)](https://github.com/wapmorgan "wapmorgan (36 commits)")[![Metallizzer](https://avatars.githubusercontent.com/u/691719?v=4)](https://github.com/Metallizzer "Metallizzer (7 commits)")[![d3pendent](https://avatars.githubusercontent.com/u/3381225?v=4)](https://github.com/d3pendent "d3pendent (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![slowbro](https://avatars.githubusercontent.com/u/327037?v=4)](https://github.com/slowbro "slowbro (1 commits)")

---

Tags

chineseenglishfrenchgermanlanguagemultilingualnatural-languagerussianspanishtimeparserparsertimedatehumanizedhuman-written

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wapmorgan-time-parser/health.svg)

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

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

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

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M732](/packages/erusev-parsedown)[league/commonmark

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

3.0k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)

PHPackages © 2026

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