Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format the datetime value in Yii 2

Tags:

php

yii2

The datetime value saved in database is "2014-06-30 02:52:51.210201".

This is also the value displayed by Yii.

What is the best approach to display it as "30-06-2014 02:52:51" everywhere?

Found this in Yii's wiki but don't know how to use it:

You can configure yii\i18n\formatter to control your global date formats for display for your locale. You can set something like this in your config file that you can access across

'formatter' => [
 'class' => 'yii\i18n\Formatter',
 'dateFormat' => 'd-M-Y',
 'datetimeFormat' => 'd-M-Y H:i:s',
 'timeFormat' => 'H:i:s', ]`

Then you can display your date times anywhere using the formatter specified formats:

echo \Yii::t('app', 'Today is {0, date}', $yourTimeStampAttr);

UPDATE

I create a custom attribute for the model to retrieve the time

public function getFormattedCreateTime()
{
    return DateTime::createFromFormat('Y-m-d H:i:s.u', $this->create_time)->format('d-m-Y H:i:s');
}

another problem though, how do I use this attribute for search in Yii 2's GridView ? thanks

SOLVED

can add the custom attribute for search by inherits the search function

like image 788
Edxz Avatar asked Jun 29 '14 20:06

Edxz


3 Answers

\Yii::$app->formatter->asDatetime("2014-06-30 02:52:51.210201", "php:d-m-Y H:i:s");

Second parameter alow you set a custom format as specified in the ICU manual. Or you can set php date() format with php: prefix.

As mentioned in Yii2 documentation the following types of value are supported:

If you have custom date string you can use DateTime::createFromFormat() function:

$dateTime = \DateTime::createFromFormat("d/m/Y  H:i:s", '31/01/2015');
\Yii::$app->formatter->asDatetime($dateTime, "php:d-m-Y  H:i:s");
like image 134
dmvslv Avatar answered Oct 19 '22 18:10

dmvslv


You have found the right solution yourself, setting the formatter in the config.

Based on what skeleton app you've used (for example official advanced template) then you add this to your config file main.php.

/your-app/config/main.php

or

/common/config/main.php

Should look like this:

<?php
return [
   'components' => [
       ...
       'formatter' => [
           'dateFormat' => 'd-M-Y',
           'datetimeFormat' => 'd-M-Y H:i:s',
           'timeFormat' => 'H:i:s',

           'locale' => 'de-DE', //your language locale
           'defaultTimeZone' => 'Europe/Berlin', // time zone
      ],
   ],
   // set target language
   'language' => 'de-DE',
],
?>

See here in the documentation for the properties you can use in formatter.

Then in your app you can use the formatter without format strings.

Yii::$app->formatter->asDate($yourDate);
Yii::$app->formatter->asDatetime($yourDatetime);
Yii::$app->formatter->asTime($yourTime);

or in data widgets (like GridView) use the "format" property

'$yourDate:date',
'$yourDatetime:datetime',
'$yourTime:time',
like image 11
mfgmicha Avatar answered Oct 19 '22 17:10

mfgmicha


if you want to show in your view then just write your code like this

<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
     [ 
      'label' => 'Modified Date',
      'value' => date("d-M-Y h:i:s",  strtotime($model->client_modified_date)),
     ]
   ],
]) ?>

its show the format of dd-mm-yy h:i:s

like image 2
Bhola Singh Avatar answered Oct 19 '22 17:10

Bhola Singh