Format date, time, and numbers

Now, let's see how to format the date, time, and numeric fields. Yii2 provides helpers for each of these types.

To format a value, we will use Yii::$app->formatter; this object belongs to the Formatter class located under yii\i18n\ and supports many types of formatting. All the methods used for this purpose start with an as prefix. Therefore, the asDate method will be used to format dates, and the asCurrency method will be used to format currencies.

The first parameter of each formatting method is the value to be formatted and other fields refer to the format to be used and other optional parameters.

Let's change the view content by adding content of the Model that is ready to be saved:

<?php if($modelCanSave) { ?> <div ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.