Formatted output data for YII framework

Source: Internet
Author: User
Tags i18n

This article mainly share with you the format output data of Yii framework, hoping to help everyone.

Format Time/Date data

Several formatting formats are supported by default

  • Date: This variable will be formatted as a date January 01, 2014 .

  • Time: This variable will be formatted as a date 14:23 .

  • DateTime: This variable will be formatted as a date + time January 01, 2014 14:23 .

  • Timestamp: This variable will be formatted as UNIX timestamp Unix timestamp, for example 1412609982 .

  • Relativetime: This variable will be formatted as human readable current relative time 1 hour ago .

  • Duration: This variable will be formatted as a human-readable length of time 1 day, 2 minutes .

Time/Date data is formatted by default by using date, times, and DateTime methods, and you can configure them in a few configurations, just configure Dateformat,timeformat in the configuration file, and DateTimeFormat.

At the same time, you can configure it to use ICU syntax, and you can also configure it to use the PHP date () syntax, just add the php: prefix.

Formatting numbers

formatterThe following methods are supported

  • Integer: This variable will be formatted as a e.g. 42

  • Decimal: This variable will be formatted as a floating-point e.g with a specified precision with a comma. 2,542.123 2.542,123

  • Percent: This variable will be formatted as percent e.g. 42%

  • Scientific: This variable will be formatted as a scientific counting method e.g. 4.2E4

  • Currency: This variable will be formatted as currency ?420.00 . Verify that the locale is configured correctly before you use this method

  • Size: This variable will be formatted as a human readable number of bytes e.g. 410 kibibytes

  • Shortsize: This variable will be formatted as a human readable number of bytes (abbreviated) size, e.g. 410 KiB

You can use DecimalSeparator and thousandseparator to make adjustments. They are formatted according to the current locale.

If you want to make more advanced configurations, you can use Yii\i18n\formatter:: $numberFormatterOptions and Yii\i18n\formatter:: $numberFormatterTextOptions, Numberformatter class to format.

For example, to adjust the maximum and minimum values for the fractional part, you can configure Yii\i18n\formatter:: $numberFormatterOptions as follows:

' Numberformatteroptions '= = [Numberformatter::min_fraction_digits =0, numberformatter::max_fraction_digits =2,]

Other formatting

In addition to time/date and number formatting, Yii also supports the following common formatting

  • Raw: In addition null to being nulldisplay formatted, output as-is.

  • Text: encoded in HTML format. This is also the default method used by the GridView DataColumn.

  • NTEXT: Encoded in HTML format, line breaks will also be converted.

  • Paragraphs: encoded in HTML format, <p> wrapped with tags.

  • HTML: This value will be filtered by htmlpurifier to protect against XSS attacks, you can add some configuration for example ['html', ['Attr.AllowedFrameTargets' => ['_blank']]] .

  • Email: This value will be converted to a mailto link.



  • Boolean: true = yes , false = no , additional configuration can be made: Yii\i18n\formatter:: $booleanFormat.

null value

null value (null ) is specially formatted. fommater defaults to format null values as (not set) corresponds to the current language. You can configure the Nulldisplay property to personalize it.

Local date formatting

formatterThe current locale is used to determine what is formatted. For the same date, different time zone configurations will have different outputs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.