|Ratings|| ||Unique User Downloads|| ||Download Rankings|
|Not yet rated by the users||Total: 187 ||All time: 8,572 This week: 326|
This class can parse time in natural language and multiple idioms.
The class takes a string with date and time written in natural language and tries to match its format according to rules for known formats.
If the class matches one of the supported format rules, it returns the parsed time as a DateTime object.
The date and time format rules are retrieved from configuration files in the JSON format.
Currently it supports Russian, English, French, German, Chinese and Spanish.
TimeParser - is a parser for date and time written in natural language for PHP.
How to use
Install through composer:
`composer require wapmorgan/time-parser`
Parse some input from user and receive a `DateTime` object.
$datetime = wapmorgan\TimeParser\TimeParser::parse(fgets(STDIN), 'all');
Second arg is a language. Applicable values:
* 'all' - scan for all available languages. Use it when you can not predict user's preferred language.
* 'english' - scan only as english time.
* array('english', 'russian') - scan as english and then the rest as russian time.
* 'nonexistent-language' - force scan with system strtotime() function. In case of strtotime() scan failure, current DateTime will be returned.
Examples of dates written in natural language that will work.
в 15:12:13 в следующий понедельник в следующем году в феврале через 15 часов через 10 минут через 11 секунд через 5 лет через 2 недели через 1 день через 10 месяцев
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 months
They both will be parsed and result DateTime will contain right date:
Sun, 03 Oct 2021 06:22:24 +0400
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.
For developing reasons you may would like to see process of parsing. To do this call related methods:
To understand, how it works, look at substrings separately:
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
Alphabetic unit values
If you set third argument of
true, TimeParser will be able to parse alphabetic values:
in fifteen hours
in ten minutes
in eleven seconds
in five years
in two weeks
in one day
in ten months
If you want to disallow users use words instead of numbers, save default settings or set third argument of
false. In this case alphabetic values will not be parsed.
Numbers up to 20 are available to use.
Library is very sensitive to all sorts of rubbish: extra spaces between numbers and prepositions, etc.
Implies that the incoming data comes in lower case.
[ ] Tests.
[ ] Try to parse combinations: in 5 hours and 2 minutes.
[x] Try to parse alphabetic offsets: in five hours and через пять часов.
[x] Chinese - check hieroglyphs.
[x] Spanish - check prepositions.
[ ] Portuguese
[ ] Arabic
[ ] Korean
||Applications that use this package
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.