php yii2:显示日期的格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28054616/
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
yii2:formatting of date for display
提问by Pawan
I have configured a date field appointment_date as datetime
field in mysql db.
我datetime
在 mysql db 中配置了一个日期字段约会日期作为字段。
In my model Appointment.php rules are set like this:
在我的模型 Appointment.php 中,规则设置如下:
public function rules()
{
return [
[['appointment_date','weekdays'], 'safe'],
[['appointment_date'], 'date','format' => 'd-M-yyyy H:m'],
and in web.php under component I have set
并在我设置的组件下的 web.php 中
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
and in my view I am trying to format the date for display like this:
在我看来,我正在尝试格式化显示日期,如下所示:
[
Yii::$app->formatter->asDatetime('appointment_date')
],
Now I am getting the error -
现在我收到错误 -
appointment_date' is not a valid date time value: DateTime::__construct(): Failed to parse time string (appointment_date) at position 0 (a): The timezone could not be found in the database
Array
(
[warning_count] => 1
[warnings] => Array
(
[6] => Double timezone specification
)
[error_count] => 3
[errors] => Array
(
[0] => The timezone could not be found in the database
[11] => Unexpected character
[12] => Double timezone specification
)
Whereas the date stored in the database is like : 2015-01-20 11:50:00
而存储在数据库中的日期就像:2015-01-20 11:50:00
If I am not formatting the date and simply keeping the attribute in view as
appointment_date
then the date is shown as 2015-01-20 11:50:00
如果我没有格式化日期并只是保持属性在视图中,
appointment_date
那么日期显示为2015-01-20 11:50:00
I want to show the date as 20-01-2015 11:50:00
我想将日期显示为 20-01-2015 11:50:00
If I am using the code like this:
如果我使用这样的代码:
[
'attribute'=>'appointment_date',
'format'=>['DateTime','php:d-m-Y H:i:s']
],
I am getting the date formatted correctly.
我得到的日期格式正确。
I want to know What I am doing wrong here in using
我想知道我在使用时做错了什么
Yii::$app->formatter->asDatetime('appointment_date')
Thanks
谢谢
回答by fl0r
I think it's just a small typo. You are passing in a string into the asDateTime
method where it needs the value
我认为这只是一个小错误。您正在将一个字符串传递到asDateTime
需要该值的方法中
Yii::$app->formatter->asDatetime('appointment_date')
should be
应该
Yii::$app->formatter->asDatetime($model->appointment_date)
Docs: http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asDatetime()-detail
文档:http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asDatetime() -detail
回答by Pawan
OK it is as simple as that, I need to use
好的就这么简单,我需要使用
'appoinment_date:date'
or
或者
'appointment_date:datetime'
and add the format in the component formatter
并在组件格式化程序中添加格式
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat'=>'php:d-M-Y H:i:s'
and now it is working fine.
现在它工作正常。
回答by S.Yadav
My DB type of date is date
so it is storing and giving response in "YYYY-DD-MM"formate.
I did this in view (index.php) without changing logic or DB Data.
我的数据库日期类型是date
这样的,它以“YYYY-DD-MM”格式存储并给出响应。
我在没有改变逻辑或数据库数据的情况下在视图 (index.php) 中做到了这一点。
initially it was
最初是
'date'
which I changed with this-
我用这个改变了——
[
'attribute' => 'date',
'value' => function ($model, $key, $index, $widget) {
return date("d-m-Y", strtotime($model->date));
},
],
It is working fine for me,
Hope it will work for few more needy.
它对我来说很好用,
希望它能为更多有需要的人服务。
回答by Dheeraj singh
Put the below code under the component section in Project/config/web.php
将以下代码放在 Project/config/web.php 的组件部分下
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat'=>'php:d-M-Y H:i:s'
]
Where you showing date ot datetimein your grid just add this For Date
您在网格中显示日期或日期时间的地方只需添加此 For Date
'date_column_name:date'
For Datetime
对于日期时间
'datetime_column_name:datetime'
That's it.It will work for your whole project where you want to change date or datetime format.
就是这样。它适用于您想要更改日期或日期时间格式的整个项目。