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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24479847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:20:31  来源:igfitidea点击:

Format the datetime value in Yii 2

phpyii2

提问by Edxz

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 wikibut 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. 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);

你可以配置 yii\i18n\formatter 来控制你的全局日期格式以显示你的语言环境。您可以在您可以访问的配置文件中设置类似的内容

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

然后您可以使用格式化程序指定的格式在任何地方显示您的日期时间:

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

另一个问题是,如何在 Yii 2 的 GridView 中使用此属性进行搜索?谢谢

SOLVED

解决了

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

可以通过继承搜索功能添加自定义属性进行搜索

回答by dmvslv

\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.

第二个参数是您设置ICU 手册中指定的自定义格式。或者您可以使用前缀设置 php date()格式php:

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");

回答by mfgmicha

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 documentationfor 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)中使用“格式”属性

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

回答by Bhola Singh

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的格式

回答by Kosmos

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

使用 jQuery 插件 datepick 和输入字段。所以,没有小部件解决问题的下一个方式:

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

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

其中$model->released_date是mysql格式时间戳的日期。

回答by Muhammad Shahzad

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

如果您只想从时间戳值中显示日期和时间,例如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

回答by Shuhad zaman

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'],
        ],
    ]); ?>