简体   繁体   English

在Yii 2中格式化日期时间值

[英]Format the datetime value in Yii 2

The datetime value saved in database is " 2014-06-30 02:52:51.210201 ". 保存在数据库中的日期时间值为“ 2014-06-30 02:52:51.210201 ”。

This is also the value displayed by Yii. 这也是Yii显示的值。

What is the best approach to display it as " 30-06-2014 02:52:51 " everywhere? 到处都是“ 30-06-2014 02:52:51 ”的最佳方法是什么?

Found this in Yii's wiki but don't know how to use it: Yii的wiki中找到了这个,但不知道如何使用它:

You can configure yii\\i18n\\formatter to control your global date formats for display for your locale. 您可以配置yii \\ i18n \\ formatter来控制您的区域设置显示的全局日期格式。 You can set something like this in your config file that you can access across 您可以在配置文件中设置可以访问的类似内容

 'formatter' => [ 'class' => 'yii\\i18n\\Formatter', 'dateFormat' => 'dM-Y', 'datetimeFormat' => 'dMY 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 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 ? 另一个问题是,如何在Yii 2的GridView中使用此属性进行搜索? thanks 谢谢

SOLVED 解决了

can add the custom attribute for search by inherits the search function 可以通过继承搜索功能为搜索添加自定义属性

\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 . 第二个参数允许您设置ICU手册中指定的自定义格式。 Or you can set php date() format with php: prefix. 或者你可以用php:前缀设置php date()格式。

As mentioned in Yii2 documentation the following types of value are supported: 如Yii2文档中所述,支持以下类型的值:

If you have custom date string you can use DateTime::createFromFormat() function: 如果您有自定义日期字符串,则可以使用DateTime :: createFromFormat()函数:

$dateTime = \DateTime::createFromFormat("d/m/Y  H:i:s", '31/01/2015');
\Yii::$app->formatter->asDatetime($dateTime, "php:d-m-Y  H:i:s");

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. 根据您使用的骨架应用程序(例如官方高级模板),然后将其添加到配置文件main.php中。

/your-app/config/main.php /your-app/config/main.php

or 要么

/common/config/main.php /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 或者在数据小部件(如GridView)中使用“format”属性

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

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 它显示的格式为dd-mm-yy h:i:s

Using jQuery plugin datepick and input field. 使用jQuery插件datepick和输入字段。 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. 其中$model->released_date是mysql格式时间戳的日期。

If you just want to show date and time from time stamp value like created_at , updated_at then! 如果您只想显示创建日期和时间值,如created_atupdated_at那么!

GridView: 网格视图:

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

Same as DetailVeiw: 与DetailVeiw相同:

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

just use this inside your GridView widget 只需在GridView小部件中使用它

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

for example 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM