class DatetimeFormatter implements FormatterInterface (View source)

Formatter for date and time.

This is not full implementation of features from CLDR. These are missing:

  • support for other calendars than Gregorian
  • rules for displaying timezone names are simplified
  • some less frequently used format characters are not supported

Properties

protected DatesReader $datesReader

Methods

void
injectDatesReader(DatesReader $datesReader)

No description

string
format(mixed $value, Locale $locale, array $styleProperties = [])

Formats provided value using optional style properties

string
formatDateTimeWithCustomPattern(DateTimeInterface $dateTime, string $format, Locale $locale)

Returns dateTime formatted by custom format, string provided in parameter.

string
formatDate(DateTimeInterface $date, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT)

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

string
formatTime(DateTimeInterface $time, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT)

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

string
formatDateTime(DateTimeInterface $dateTime, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT)

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

string
doFormattingWithParsedFormat(DateTimeInterface $dateTime, array $parsedFormat, array $localizedLiterals)

Formats provided dateTime object.

string
doFormattingForSubpattern(DateTimeInterface $dateTime, string $subformat, array $localizedLiterals)

Formats date or time element according to the subpattern provided.

string
padString(string $string, int $formatLength)

Pads given string to the specified length with zeros.

Details

void injectDatesReader(DatesReader $datesReader)

No description

Parameters

DatesReader $datesReader

Return Value

void

string format(mixed $value, Locale $locale, array $styleProperties = [])

Formats provided value using optional style properties

Parameters

mixed $value

Formatter-specific variable to format (can be integer, \DateTime, etc)

Locale $locale

Locale to use

array $styleProperties

Integer-indexed array of formatter-specific style properties (can be empty)

Return Value

string

String representation of $value provided, or (string)$value

string formatDateTimeWithCustomPattern(DateTimeInterface $dateTime, string $format, Locale $locale)

Returns dateTime formatted 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 this classes cache and won't be parsed again for some time.

Parameters

DateTimeInterface $dateTime

PHP object representing particular point in time

string $format

Format string

Locale $locale

A locale used for finding literals array

Return Value

string

Formatted date / time. Unimplemented subformats in format string will be silently ignored

See also

DatesReader

string formatDate(DateTimeInterface $date, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT)

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

Parameters

DateTimeInterface $date

PHP object representing particular point in time

Locale $locale
string $formatLength

One of DatesReader FORMAT_LENGTH constants

Return Value

string

Formatted date

string formatTime(DateTimeInterface $time, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT)

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

Parameters

DateTimeInterface $time

PHP object representing particular point in time

Locale $locale
string $formatLength

One of DatesReader FORMAT_LENGTH constants

Return Value

string

Formatted time

string formatDateTime(DateTimeInterface $dateTime, Locale $locale, string $formatLength = DatesReader::FORMAT_LENGTH_DEFAULT)

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

First date and time are formatted separately, and then dateTime format from CLDR is used to place date and time in correct order.

Parameters

DateTimeInterface $dateTime

PHP object representing particular point in time

Locale $locale
string $formatLength

One of DatesReader FORMAT_LENGTH constants

Return Value

string

Formatted date and time

protected string doFormattingWithParsedFormat(DateTimeInterface $dateTime, array $parsedFormat, array $localizedLiterals)

Formats provided dateTime object.

Format rules defined in $parsedFormat array are used. Localizable literals are replaced with elements from $localizedLiterals array.

Parameters

DateTimeInterface $dateTime

PHP object representing particular point in time

array $parsedFormat

An array describing format (as in $parsedFormats property)

array $localizedLiterals

An array with literals to use (as in $localizedLiterals property)

Return Value

string

Formatted date / time

protected string doFormattingForSubpattern(DateTimeInterface $dateTime, string $subformat, array $localizedLiterals)

Formats date or time element according to the subpattern provided.

Returns a string with formatted one "part" of DateTime object (seconds, day, month etc).

Not all pattern symbols defined in CLDR are supported; some of the rules are simplified. Please see the documentation for DatesReader for details.

Cases in the code are ordered in such way that probably mostly used are on the top (but they are also grouped by similarity).

Parameters

DateTimeInterface $dateTime

PHP object representing particular point in time

string $subformat

One element of format string (e.g., 'yyyy', 'mm', etc)

array $localizedLiterals

Array of date / time literals from CLDR

Return Value

string

Formatted part of date / time

Exceptions

InvalidArgumentException

See also

DatesReader

protected string padString(string $string, int $formatLength)

Pads given string to the specified length with zeros.

Parameters

string $string
int $formatLength

Return Value

string

Padded string (can be unchanged if $formatLength is lower than length of string)