পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, March 22, 2017

Yii - Formatting

To display data in a readable format, you can use the formatter application component.
Step1 − Add the actionFormatter method to the SiteController.
public 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.
View File 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.
Formatter 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.
Data Formats 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.
Number Formats 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 newlines are converted into line 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 link.
  • email − The value is formatted as a mailto-link.
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');
?>

No comments:

Post a Comment