16

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

5
  • Whether milliseconds are necessary to you in general?
    – frops
    Jun 30, 2014 at 15:34
  • @frops: it's the default format in the postgres
    – Edxz
    Jun 30, 2014 at 15:39
  • Wiki Link is missing! Sep 16, 2014 at 15:42
  • ok found and added it! Sep 16, 2014 at 15:43
  • Can you give an exemple of the inherited search ?
    – Ydakilux
    Feb 4, 2015 at 7:58

6 Answers 6

21
\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");
3
  • Could you please explain why this is good solution to the problem. Jun 30, 2014 at 10:43
  • @dmvslv This works ! but the value displayed is : '30-52-2014 02::', and the format that I use is 'd-m-Y H:i:s', what's the problem ?
    – Edxz
    Jun 30, 2014 at 15:10
  • Its not working with format 31/01/2015 i.e. for / separator. Oct 11, 2015 at 18:17
11

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',
1
  • $yourDatetime:datetime this shows the date time value with Hour field increased by 1 hour. for example if datetime value from table is 2016-03-15 09:00:02 then this code displays Mar 15, 2016, 10:00:02 AM
    – Choxx
    Mar 19, 2016 at 6:45
2

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

2
  • You could try client_modified_date:date for value. This will take the format from formatter component you configured in your app config. The pattern is attribute:format:label
    – SenG
    Jun 19, 2015 at 4:17
  • you should wrap value expression into anonymous function if you going to use $model. otherwise there will be an error. Jun 18, 2019 at 10:46
2

Using jQuery plugin datepick and input field. So, without widget resolved problem the next way:

<?= $form->field($model, 'released_date')->textInput(['maxlenght' => 255, 'value' => date( 'd-M-Y', strtotime( $model->released_date ) )]); ?>

where $model->released_date is a date in mysql format timestamp.

0

If you just want to show date and time from time stamp value like created_at,updated_at then!

GridView:

'created_at:datetime', // Jul 28, 2016, 8:29:38 PM
'updated_at:datetime', // Jul 28, 2016, 8:29:38 PM

Same as DetailVeiw:

'created_at:datetime', // Jul 28, 2016, 8:29:38 PM
'updated_at:datetime', // Jul 28, 2016, 8:29:38 PM
0

just use this inside your GridView widget

'$yourDate:date',
'$yourDatetime:datetime',
'$yourTime:time'

for example

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'id',
            '$yourDate:date',
['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.