Craft 3 Class Reference

Class craft\i18n\Formatter

Inheritance
craft\i18n\Formatter » yii\i18n\Formatter » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/i18n/Formatter.php

Public Properties
Property Type Description Defined By
$amName string, null The localized AM name. craft\i18n\Formatter
$baseUnits array The base units that are used as multipliers for smallest possible unit from $measureUnits. yii\i18n\Formatter
$booleanFormat array The text to be displayed when formatting a boolean value. yii\i18n\Formatter
$calendar IntlCalendar, integer, null The calendar to be used for date formatting. yii\i18n\Formatter
$currencyCode string The 3-letter ISO 4217 currency code indicating the default currency to use for asCurrency(). yii\i18n\Formatter
$currencySymbols array, null The locale's currency symbols. craft\i18n\Formatter
$dateFormat string The default format string to be used to format a date. yii\i18n\Formatter
$dateTimeFormats array The locale’s date/time formats. craft\i18n\Formatter
$datetimeFormat string The default format string to be used to format a date and time. yii\i18n\Formatter
$decimalSeparator string The character displayed as the decimal point when formatting a number. yii\i18n\Formatter
$defaultTimeZone string The time zone that is assumed for input values if they do not include a time zone explicitly. yii\i18n\Formatter
$locale string The locale ID that is used to localize the date and number formatting. yii\i18n\Formatter
$measureUnits array Configuration of weight and length measurement units. yii\i18n\Formatter
$monthNames array, null The localized month names. craft\i18n\Formatter
$nullDisplay string The text to be displayed when formatting a null value. yii\i18n\Formatter
$numberFormatterOptions array A list of name value pairs that are passed to the intl [NumberFormatter::setAttribute()](http://php. yii\i18n\Formatter
$numberFormatterSymbols array A list of name value pairs that are passed to the intl [NumberFormatter::setSymbol()](http://php. yii\i18n\Formatter
$numberFormatterTextOptions array A list of name value pairs that are passed to the intl [NumberFormatter::setTextAttribute()](http://php. yii\i18n\Formatter
$pmName string, null The localized PM name. craft\i18n\Formatter
$sizeFormatBase integer The base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte), used by asSize() and asShortSize(). yii\i18n\Formatter
$standAloneMonthNames array, null The localized "stand alone" month names. craft\i18n\Formatter
$standAloneWeekDayNames array, null The localized "stand alone" day of the week names. craft\i18n\Formatter
$systemOfUnits string Default system of measure units. yii\i18n\Formatter
$thousandSeparator string The character displayed as the thousands separator (also called grouping separator) character when formatting a number. yii\i18n\Formatter
$timeFormat string The default format string to be used to format a time. yii\i18n\Formatter
$timeZone string The time zone to use for formatting time and date values. yii\i18n\Formatter
$weekDayNames array, null The localized day of the week names. craft\i18n\Formatter
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
asBoolean() Formats the value as a boolean. yii\i18n\Formatter
asCurrency() Formats the value as a currency number. craft\i18n\Formatter
asDate() Formats the value as a date. craft\i18n\Formatter
asDatetime() Formats the value as a datetime. craft\i18n\Formatter
asDecimal() Formats the value as a decimal number. yii\i18n\Formatter
asDuration() Represents the value as duration in human readable format. yii\i18n\Formatter
asEmail() Formats the value as a mailto link. yii\i18n\Formatter
asHtml() Formats the value as HTML text. yii\i18n\Formatter
asImage() Formats the value as an image tag. yii\i18n\Formatter
asInteger() Formats the value as an integer number by removing any decimal digits without rounding. yii\i18n\Formatter
asLength() Formats the value as a length in human readable form for example 12 meters. yii\i18n\Formatter
asNtext() Formats the value as an HTML-encoded plain text with newlines converted into breaks. yii\i18n\Formatter
asOrdinal() Formats the value as a ordinal value of a number. yii\i18n\Formatter
asParagraphs() Formats the value as HTML-encoded text paragraphs. yii\i18n\Formatter
asPercent() Formats the value as a percent number with "%" sign. yii\i18n\Formatter
asRaw() Formats the value as is without any formatting. yii\i18n\Formatter
asRelativeTime() Formats the value as the time interval between a date and now in human readable form. yii\i18n\Formatter
asScientific() Formats the value as a scientific number. yii\i18n\Formatter
asShortLength() Formats the value as a length in human readable form for example 12 m. yii\i18n\Formatter
asShortSize() Formats the value in bytes as a size in human readable form for example 12 KB. yii\i18n\Formatter
asShortWeight() Formats the value as a weight in human readable form for example 12 kg. yii\i18n\Formatter
asSize() Formats the value in bytes as a size in human readable form, for example 12 kilobytes. yii\i18n\Formatter
asSpellout() Formats the value as a number spellout. yii\i18n\Formatter
asText() Formats the value as an HTML-encoded plain text. craft\i18n\Formatter
asTime() Formats the value as a time. craft\i18n\Formatter
asTimestamp() Formats the value as a human-readable timestamp. craft\i18n\Formatter
asUrl() Formats the value as a hyperlink. yii\i18n\Formatter
asWeight() Formats the value as a weight in human readable form for example 12 kilograms. yii\i18n\Formatter
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
format() Formats the value based on the given format type. yii\i18n\Formatter
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
trigger() Triggers an event. yii\base\Component
Protected Methods
Method Description Defined By
createNumberFormatter() Creates a number formatter based on the given type and format. yii\i18n\Formatter
normalizeDatetimeValue() Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods. yii\i18n\Formatter
normalizeNumericValue() Normalizes a numeric input value. yii\i18n\Formatter
Constants
Constant Value Description Defined By
FORMAT_WIDTH_LONG 'long' yii\i18n\Formatter
FORMAT_WIDTH_SHORT 'short' yii\i18n\Formatter
UNIT_LENGTH 'length' yii\i18n\Formatter
UNIT_SYSTEM_IMPERIAL 'imperial' yii\i18n\Formatter
UNIT_SYSTEM_METRIC 'metric' yii\i18n\Formatter
UNIT_WEIGHT 'mass' yii\i18n\Formatter

Property Details

$amName public property #

The localized AM name.

public string, null $amName null

$currencySymbols public property #

The locale's currency symbols.

public array, null $currencySymbols null

$dateTimeFormats public property #

The locale’s date/time formats.

public array $dateTimeFormats null

$monthNames public property #

The localized month names.

public array, null $monthNames null

$pmName public property #

The localized PM name.

public string, null $pmName null

$standAloneMonthNames public property #

The localized "stand alone" month names.

public array, null $standAloneMonthNames null

$standAloneWeekDayNames public property #

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

public array, null $standAloneWeekDayNames null

$weekDayNames public property #

The localized day of the week names.

public array, null $weekDayNames null

Method Details

asCurrency() public method #

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.

public string asCurrency ( $value, $currency null, $options = [], $textOptions = [], $stripZeros false )
$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).
return string The formatted result.
throws yii\base\InvalidArgumentException if the input value is not numeric.
throws yii\base\InvalidConfigException if no currency is given and $currencyCode is not defined.

asDate() public method #

Formats the value as a date.

public string asDate ( $value, $format null )
$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.
return string The formatted result.
throws yii\base\InvalidArgumentException
throws yii\base\InvalidConfigException

asDatetime() public method #

Formats the value as a datetime.

public string asDatetime ( $value, $format null )
$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, $datetimeFormat 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.
return string The formatted result.
throws yii\base\InvalidArgumentException
throws yii\base\InvalidConfigException

asText() public method #

Formats the value as an HTML-encoded plain text.

public string asText ( $value )
$value string The value to be formatted.
return string The formatted result.

asTime() public method #

Formats the value as a time.

public string asTime ( $value, $format null )
$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.
return string The formatted result.
throws yii\base\InvalidArgumentException
throws yii\base\InvalidConfigException

asTimestamp() public method #

Formats the value as a human-readable timestamp.

See also $datetimeFormat.

public string asTimestamp ( $value, \craft\i18n\string $format null )
$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.
return string The formatted result.
throws yii\base\InvalidArgumentException if the input value can not be evaluated as a date value.
throws yii\base\InvalidConfigException if the date format is invalid.