class DatetimeParser (View source)

Parser for date and time.

Constants

PATTERN_MATCH_STRICT_TIMEZONE_ABBREVIATION

Regex pattern for matching abbreviated timezones, like GMT, CEST, etc.

Two versions for strict and lenient matching modes.

PATTERN_MATCH_LENIENT_TIMEZONE_ABBREVIATION

PATTERN_MATCH_STRICT_TIMEZONE_TZ

Regex pattern for matching TZ database timezones, like Europe/London.

Two versions for strict and lenient matching modes.

PATTERN_MATCH_LENIENT_TIMEZONE_TZ

Properties

protected DatesReader $datesReader

Methods

void
injectDatesReader(DatesReader $datesReader)

No description

mixed
parseDatetimeWithCustomPattern(string $datetimeToParse, string $format, Locale $locale, bool $strictMode = true)

Returns dateTime parsed by custom format (string provided in parameter).

mixed
parseDate(string $dateToParse, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses date with format string for date defined in CLDR for particular locale.

mixed
parseTime(string $timeToParse, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses time with format string for time defined in CLDR for particular locale.

mixed
parseDateAndTime(string $dateAndTimeToParse, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses dateTime with format string for date and time defined in CLDR for particular locale.

mixed
doParsingWithParsedFormat(string $datetimeToParse, array $parsedFormat, array $localizedLiterals, bool $strictMode)

Parses date and / or time using parsed format, in strict or lenient mode.

array
doParsingInStrictMode(string $datetimeToParse, array $parsedFormat, array $localizedLiterals)

Parses date and / or time in strict mode.

array
doParsingInLenientMode(string $datetimeToParse, array $parsedFormat, array $localizedLiterals)

Parses date and / or time in lenient mode.

int
extractAndCheckNumber(string $datetimeToParse, bool $isTwoDigits, int $minValue, int $maxValue)

Extracts one or two-digit number from the beginning of the string.

string
extractNumberAndGetPosition(string $datetimeToParse, int $position)

Extracts and returns first integer number encountered in provided string.

Details

void injectDatesReader(DatesReader $datesReader)

No description

Parameters

DatesReader $datesReader

Return Value

void

mixed parseDatetimeWithCustomPattern(string $datetimeToParse, string $format, Locale $locale, bool $strictMode = true)

Returns dateTime parsed by custom format (string provided in parameter).

Format must obey syntax defined in CLDR specification, excluding unimplemented features (see documentation for DatesReader class).

Format is remembered in cache and won't be parsed again for some time.

Parameters

string $datetimeToParse

Date/time to be parsed

string $format

Format string

Locale $locale

A locale used for finding literals array

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Array of parsed date / time elements, false on failure

See also

DatesReader

mixed parseDate(string $dateToParse, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses date with format string for date defined in CLDR for particular locale.

Parameters

string $dateToParse

date to be parsed

Locale $locale
string $formatLength

One of: full, long, medium, short, or 'default' in order to use default length from CLDR

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Array of parsed date elements, false on failure

mixed parseTime(string $timeToParse, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses time with format string for time defined in CLDR for particular locale.

Parameters

string $timeToParse

Time to be parsed

Locale $locale
string $formatLength

One of: full, long, medium, short, or 'default' in order to use default length from CLDR

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Array of parsed time elements, false on failure

mixed parseDateAndTime(string $dateAndTimeToParse, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT, bool $strictMode = true)

Parses dateTime with format string for date and time defined in CLDR for particular locale.

Parameters

string $dateAndTimeToParse

Date and time to be parsed

Locale $locale
string $formatLength

One of: full, long, medium, short, or 'default' in order to use default length from CLDR

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Array of parsed date and time elements, false on failure

protected mixed doParsingWithParsedFormat(string $datetimeToParse, array $parsedFormat, array $localizedLiterals, bool $strictMode)

Parses date and / or time using parsed format, in strict or lenient mode.

Parameters

string $datetimeToParse

Date/time to be parsed

array $parsedFormat

Parsed format (from DatesReader)

array $localizedLiterals

Array of date / time literals from CLDR

bool $strictMode

Work mode (strict when true, lenient when false)

Return Value

mixed

Array of parsed date and / or time elements, false on failure

protected array doParsingInStrictMode(string $datetimeToParse, array $parsedFormat, array $localizedLiterals)

Parses date and / or time in strict mode.

Parameters

string $datetimeToParse

Date/time to be parsed

array $parsedFormat

Format parsed by DatesReader

array $localizedLiterals

Array of date / time literals from CLDR

Return Value

array

Array of parsed date and / or time elements, false on failure

Exceptions

InvalidArgumentException

See also

DatesReader

protected array doParsingInLenientMode(string $datetimeToParse, array $parsedFormat, array $localizedLiterals)

Parses date and / or time in lenient mode.

Algorithm assumptions:

  • ignore all literals
  • order of elements in parsed format is important
  • length of subformat is not strictly checked (eg. 'h' and 'hh')
  • number must be in range in order to be accepted (eg. 1-12 for month)
  • some format fallback substitutions can be done (eg. 'Jan' for 'January')

Parameters

string $datetimeToParse

Date/time to be parsed

array $parsedFormat

Format parsed by DatesReader

array $localizedLiterals

Array of date / time literals from CLDR

Return Value

array

Array of parsed date and / or time elements (can be array of NULLs if nothing was parsed)

Exceptions

InvalidParseStringException
InvalidArgumentException

See also

DatesReader

protected int extractAndCheckNumber(string $datetimeToParse, bool $isTwoDigits, int $minValue, int $maxValue)

Extracts one or two-digit number from the beginning of the string.

If the number has certainly two digits, $isTwoDigits can be set to true so no additional checking is done (this implies from some date/time formats, like 'hh').

Number is also checked for constraints: minimum and maximum value.

Parameters

string $datetimeToParse

Date/time to be parsed

bool $isTwoDigits

true if number has surely two digits, false if it has one or two digits

int $minValue
int $maxValue

Return Value

int

Parsed number

Exceptions

InvalidParseStringException

protected string extractNumberAndGetPosition(string $datetimeToParse, int $position)

Extracts and returns first integer number encountered in provided string.

Searches for first digit and extracts all adjacent digits. Also returns position of first digit in string.

Parameters

string $datetimeToParse

String to search number in

int $position

Index of first digit in string

Return Value

string

Extracted number

Exceptions

InvalidParseStringException