Formatter

Type
Class
Namespace
craft\i18n
Inherits
craft\i18n\Formatter » yii\i18n\Formatter » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0

View source

Public Properties

PropertyDescription
$amNamestring, null – The localized AM name.
$baseUnitsarray – The base units that are used as multipliers for smallest possible unit from $measureUnits.
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$booleanFormatarray – The text to be displayed when formatting a boolean value.
$calendar\IntlCalendar, integer, null – The calendar to be used for date formatting.
$currencyCodestring – The 3-letter ISO 4217 currency code indicating the default currency to use for asCurrency().
$currencySymbolsarray, null – The locale's currency symbols.
$dateFormatstring – The default format string to be used to format a date.
$dateTimeFormatsarray – The locale’s date/time formats.
$datetimeFormatstring – The default format string to be used to format a date and time.
$decimalSeparatorstring – The character displayed as the decimal point when formatting a number.
$defaultTimeZonestring – The time zone that is assumed for input values if they do not include a time zone explicitly.
$localestring – The locale ID that is used to localize the date and number formatting.
$measureUnitsarray – Configuration of weight and length measurement units.
$monthNamesarray, null – The localized month names.
$nullDisplaystring – The text to be displayed when formatting a null value.
$numberFormatterOptionsarray – A list of name value pairs that are passed to the intl [NumberFormatter::setAttribute()](http://php.
$numberFormatterSymbolsarray – A list of name value pairs that are passed to the intl [NumberFormatter::setSymbol()](http://php.
$numberFormatterTextOptionsarray – A list of name value pairs that are passed to the intl [NumberFormatter::setTextAttribute()](http://php.
$pmNamestring, null – The localized PM name.
$sizeFormatBaseinteger – The base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte), used by asSize() and asShortSize().
$standAloneMonthNamesarray, null – The localized "stand alone" month names.
$standAloneWeekDayNamesarray, null – The localized "stand alone" day of the week names.
$systemOfUnitsstring – Default system of measure units.
$thousandSeparatorstring – The character displayed as the thousands separator (also called grouping separator) character when formatting a number.
$timeFormatstring – The default format string to be used to format a time.
$timeZonestring – The time zone to use for formatting time and date values.
$weekDayNamesarray, null – The localized day of the week names.

$amName

Type
string, null

The localized AM name.

View source

Signature

public string, null $amName = null

$currencySymbols

Type
array, null

The locale's currency symbols.

View source

Signature

public array, null $currencySymbols = null

$dateTimeFormats

Type
array

The locale’s date/time formats.

View source

Signature

public array $dateTimeFormats = null

$monthNames

Type
array, null

The localized month names.

View source

Signature

public array, null $monthNames = null

$pmName

Type
string, null

The localized PM name.

View source

Signature

public string, null $pmName = null

$standAloneMonthNames

Type
array, null

The localized "stand alone" month names.

View source

Signature

public array, null $standAloneMonthNames = null

$standAloneWeekDayNames

Type
array, null

The localized "stand alone" day of the week names.

View source

Signature

public array, null $standAloneWeekDayNames = null

$weekDayNames

Type
array, null

The localized day of the week names.

View source

Signature

public array, null $weekDayNames = null

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to null.
asBoolean()Formats the value as a boolean.
asCurrency()Formats the value as a currency number.
asDate()Formats the value as a date.
asDatetime()Formats the value as a datetime.
asDecimal()Formats the value as a decimal number.
asDuration()Represents the value as duration in human readable format.
asEmail()Formats the value as a mailto link.
asHtml()Formats the value as HTML text.
asImage()Formats the value as an image tag.
asInteger()Formats the value as an integer number by removing any decimal digits without rounding.
asLength()Formats the value as a length in human readable form for example 12 meters.
asNtext()Formats the value as an HTML-encoded plain text with newlines converted into breaks.
asOrdinal()Formats the value as a ordinal value of a number.
asParagraphs()Formats the value as HTML-encoded text paragraphs.
asPercent()Formats the value as a percent number with "%" sign.
asRaw()Formats the value as is without any formatting.
asRelativeTime()Formats the value as the time interval between a date and now in human readable form.
asScientific()Formats the value as a scientific number.
asShortLength()Formats the value as a length in human readable form for example 12 m.
asShortSize()Formats the value in bytes as a size in human readable form for example 12 KB.
asShortWeight()Formats the value as a weight in human readable form for example 12 kg.
asSize()Formats the value in bytes as a size in human readable form, for example 12 kilobytes.
asSpellout()Formats the value as a number spellout.
asText()Formats the value as an HTML-encoded plain text.
asTime()Formats the value as a time.
asTimestamp()Formats the value as a human-readable timestamp.
asUrl()Formats the value as a hyperlink.
asWeight()Formats the value as a weight in human readable form for example 12 kilograms.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
format()Formats the value based on the given format type.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
trigger()Triggers an event.

asCurrency()

Formats the value as a currency number. This function does not requires the PHP intl extension to be installed to work but it is highly recommended to install it to get good formatting results.

View source

Arguments

  • $value (mixed) – The value to be formatted.
  • $currency (string, null) – The 3-letter ISO 4217 currency code indicating the currency to use. If null, $currencyCode will be used.
  • $options (array) – Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions.
  • $textOptions (array) – Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions.
  • $stripZeros (boolean) – Whether the formatted currency should remove the fraction digits if $value has no minor value (e.g. cents).

Returns

string – The formatted result.

Throws

Signature

public string asCurrency ( $value, $currency = null, $options = [], $textOptions = [], $stripZeros = false )

asDate()

Formats the value as a date.

View source

Arguments

  • $value (integer, string, DateTime) – The value to be formatted. The following types of value are supported:

  • an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition.

  • a string that can be parsed to create a DateTime object. The timestamp is assumed to be in $defaultTimeZone unless a time zone is explicitly given.

  • a PHP DateTime object. You may set the time zone for the DateTime object to specify the source time zone.

The formatter will convert date values according to $timeZone before formatting it. If no timezone conversion should be performed, you need to set $defaultTimeZone and $timeZone to the same value. Also no conversion will be performed on values that have no time information, e.g. "2017-06-05".

  • $format (string, null) – The format used to convert the value into a date string. If null, $dateFormat will be used.

This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the ICU manual.

Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function.

Returns

string – The formatted result.

Throws

Signature

public string asDate ( $value, $format = null )

asDatetime()

Formats the value as a datetime.

View source

Arguments

  • $value (integer, string, DateTime) – The value to be formatted. The following types of value are supported:

  • an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition.

  • a string that can be parsed to create a DateTime object. The timestamp is assumed to be in $defaultTimeZone unless a time zone is explicitly given.

  • a PHP DateTime object. You may set the time zone for the DateTime object to specify the source time zone.

The formatter will convert date values according to $timeZone before formatting it. If no timezone conversion should be performed, you need to set $defaultTimeZone and $timeZone to the same value.

This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the ICU manual.

Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function.

Returns

string – The formatted result.

Throws

Signature

public string asDatetime ( $value, $format = null )

asText()

Formats the value as an HTML-encoded plain text.

View source

Arguments

  • $value (string) – The value to be formatted.

Returns

string – The formatted result.

Signature

public string asText ( $value )

asTime()

Formats the value as a time.

View source

Arguments

  • $value (integer, string, DateTime) – The value to be formatted. The following types of value are supported:

  • an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition.

  • a string that can be parsed to create a DateTime object. The timestamp is assumed to be in $defaultTimeZone unless a time zone is explicitly given.

  • a PHP DateTime object. You may set the time zone for the DateTime object to specify the source time zone.

The formatter will convert date values according to $timeZone before formatting it. If no timezone conversion should be performed, you need to set $defaultTimeZone and $timeZone to the same value.

  • $format (string, null) – The format used to convert the value into a date string. If null, $timeFormat will be used.

This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the ICU manual.

Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function.

Returns

string – The formatted result.

Throws

Signature

public string asTime ( $value, $format = null )

asTimestamp()

Formats the value as a human-readable timestamp.

  • If $value is from today, "Today" or the formatted time will be returned, depending on whether $value contains time information
  • If $value is from yesterday, "Yesterday" will be returned
  • If $value is within the past 7 days, the weekday will be returned See also $datetimeFormatView source

Arguments

  • $value (integer, string, DateTime) – The value to be formatted. The following types of value are supported:
  • an int representing a UNIX timestamp
  • a string that can be parsed to create a DateTime object. The timestamp is assumed to be in $defaultTimeZone unless a time zone is explicitly given.
  • a PHP DateTime object
  • $format (string, null) – The format used to convert the value into a date string. If null, $dateFormat will be used. This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the ICU manual. Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function.

Returns

string – The formatted result.

Throws

Signature

public string asTimestamp ( $value, \craft\i18n\string $format = null )

Protected Methods

MethodDescription
createNumberFormatter()Creates a number formatter based on the given type and format.
normalizeDatetimeValue()Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods.
normalizeNumericValue()Normalizes a numeric input value.

Constants

ConstantDescription
FORMAT_WIDTH_LONG
FORMAT_WIDTH_SHORT
UNIT_LENGTH
UNIT_SYSTEM_IMPERIAL
UNIT_SYSTEM_METRIC
UNIT_WEIGHT