bambinou
(Benoitrivaux)
March 5, 2015, 11:52pm
1
Hello,
I am not understanding how to pass the values of the formater inside a widget please.
For example, if I have this widget:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'post:ntext',
'created_at:datetime',
'updated_at:datetime',
],
]) ?>
And would like to pass this into the created_at and updated_at:
<?php Yii::$app->formatter->locale = ‘en-US’;
echo Yii::$app->formatter->asDate('2014-01-01'); ?>
How to do it please?
Thank you,
Ben
timmy78
(Timothee Planchais)
March 6, 2015, 10:34am
2
Like this :
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'post:ntext',
[
'attribute' => 'created_at',
'value' => function($model, $key, $index, $widget) {
return Yii::$app->formatter->asDate($model->created_at, 'long');
}
],
...
],
]) ?>
And set your locale in the config (config/web.php) :
...
'formatter' => [
'class' => 'yii\i18n\Formatter',
'locale' => 'en-US'
],
...
But it’s not necessary because by default it takes the language if the application (Yii::$app->language)
pkjoshi
(Info)
March 6, 2015, 9:16pm
3
I think your code
'created_at:datetime',
'updated_at:datetime',
is perfectly valid and it is working for me perfectly.
I think you need proper directive in your config file for example - mine is like this:
'formatter' => [
'defaultTimeZone' => 'Asia/Kolkata',
'timeZone' => 'Asia/Kolkata',
'dateFormat' => 'php:d/m/Y',
'datetimeFormat'=>'php:d-M-Y H:i:s'
],
bambinou
(Benoitrivaux)
March 6, 2015, 10:31pm
4
Thank you for your help,
I have no idea how you got it to work with the formater as I have added this code in the config/main.php file but still not results, the date is showing in the wrong format:
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'formatter' => [
'defaultTimeZone' => 'Europe/Malta',
'timeZone' => 'Europe/Malta',
'dateFormat' => 'php:d/m/Y',
'datetimeFormat'=>'php:d-M-Y H:i:s',
'currencyCode' => 'EUR',
'decimalSeparator' => ',',
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
The date is showing in this format:
Mar 7, 2015 12:24:07 AM
But I need it as:
7 Mar, 2015…
I will try the solution of Timmy78 but would have preferred to have your solution working Pawan as it would have set everything in the whole app with only a tiny bit piece of code each time in the array.
Thanks,
Ben
http://pastebin.com/mtVxi9Gt