- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Formatting
To display data in a readable format, you can use the formatter apppcation component.
Step1 − Add the actionFormatter method to the SiteController.
pubpc function actionFormatter(){ return $this->render( formatter ); }
In the above code, we just render the formatter view.
Step 2 − Now, create a formatter.php view file inside the views/site folder.
<?php $formatter = Yii::$app->formatter; // output: January 1, 2016 echo $formatter->asDate( 2016-01-01 , long ),"<br>"; // output: 51.50% echo $formatter->asPercent(0.515, 2),"<br>"; // output: <a href = "mailto:test@test.com">test@test.com</a> echo $formatter->asEmail( test@test.com ),"<br>"; // output: Yes echo $formatter->asBoolean(true),"<br>"; // output: (Not set) echo $formatter->asDate(null),"<br>"; ?>
Step 3 − Go to http://localhost:8080/index.php?r=site/formatter, you will see the following output.
The formatter component supports the following formats related with date and time −
Output format | Example |
---|---|
date | January 01, 2016 |
time | 16:06 |
datetime | January 01, 2016 16:06 |
timestamp | 1512609983 |
relativeTime | 1 hour ago |
duration | 5 minutes |
Step 4 − Modify the formatter view this way.
<?php $formatter = Yii::$app->formatter; echo $formatter->asDate(date( Y-m-d ), long ),"<br>"; echo $formatter->asTime(date("Y-m-d")),"<br>"; echo $formatter->asDatetime(date("Y-m-d")),"<br>"; echo $formatter->asTimestamp(date("Y-m-d")),"<br>"; echo $formatter->asRelativeTime(date("Y-m-d")),"<br>"; ?>
Step 5 − Type http://localhost:8080/index.php?r=site/formatter in the address bar of your web browser, you will see the following output.
Date Formats
There are also four date format shortcuts: short, medium, long, and full.
Step 1 − Modify the formatter view file this way.
<?php $formatter = Yii::$app->formatter; echo $formatter->asDate(date( Y-m-d ), short ),"<br>"; echo $formatter->asDate(date( Y-m-d ), medium ),"<br>"; echo $formatter->asDate(date( Y-m-d ), long ),"<br>"; echo $formatter->asDate(date( Y-m-d ), full ),"<br>"; ?>
Step 2 − If you go to the web browser and type http://localhost:8080/index.php?r=site/formatter, you will see the following output.
Number Formats
The formatter component supports the following formats related with numbers −
Output format | Example |
---|---|
integer | 51 |
decimal | 105.51 |
percent | 51% |
scientific | 1.050000E+2 |
currency | $105 |
size | 105 bytes |
shortSize | 105 B |
Step 1 − Modify the formatter view this way.
<?php $formatter = Yii::$app->formatter; echo Yii::$app->formatter->asInteger(105),"<br>"; echo Yii::$app->formatter->asDecimal(105.41),"<br>"; echo Yii::$app->formatter->asPercent(0.51),"<br>"; echo Yii::$app->formatter->asScientific(105),"<br>"; echo Yii::$app->formatter->asCurrency(105, "$"),"<br>"; echo Yii::$app->formatter->asSize(105),"<br>"; echo Yii::$app->formatter->asShortSize(105),"<br>"; ?>
Step 2 − Go to http://localhost:8080/index.php?r=site/formatter, you will see the following output.
Other Formats
Yii also supports other formats −
text − The value is HTML-encoded.
raw − The value is outputted as is.
paragraphs − The value is formatted as HTML text paragraphs wrapped into the p tag.
ntext − The value is formatted as an HTML plain text where newpnes are converted into pne breaks.
html − The value is purified using HtmlPurifier to avoid XSS attacks.
image − The value is formatted as an image tag.
boolean − The value is formatted as a boolean.
url − The value is formatted as a pnk.
email − The value is formatted as a mailto-pnk.
The formatter may use the currently active locale to determine how to format a value for a specific country.
The following example shows how to format date for different locales.
<?php Yii::$app->formatter->locale = ru-RU ; echo Yii::$app->formatter->asDate( 2016-01-01 ); // output: 1 января 2016 г. Yii::$app->formatter->locale = de-DE ; // output: 1. Januar 2016 echo Yii::$app->formatter->asDate( 2016-01-01 ); Yii::$app->formatter->locale = en-US ; // output: January 1, 2016 echo Yii::$app->formatter->asDate( 2016-01-01 ); ?>Advertisements