我已经在mysql db中将日期字段appointment_date配置为datetime
字段。yii2:显示日期的格式
在我的模型Appointment.php设置规则是这样的:
public function rules()
{
return [
[['appointment_date','weekdays'], 'safe'],
[['appointment_date'], 'date','format' => 'd-M-yyyy H:m'],
,并在组件web.php我已经设置
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
在我看来,我试图格式化日期显示是这样的:
[
Yii::$app->formatter->asDatetime('appointment_date')
],
现在我得到的错误 -
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
)
而存储在数据库中的日期是这样的:2015年1月20日11:50:00
如果我不格式化日期和简单地保持在属性视图 appointment_date
然后将日期如图2015-01-20 11:50:00
我想说明如果我使用这样的代码日期为20-01-2015 11:50:00
:
[
'attribute'=>'appointment_date',
'format'=>['DateTime','php:d-m-Y H:i:s']
],
我得到的日期格式正确。
我想知道我做错了在这里使用
Yii::$app->formatter->asDatetime('appointment_date')
感谢
@ fl0r-你的建议也是正确的,但我需要使用像'attribute =>'appointment_date','value'=> Yii :: $ app-> formatter-> asDatetime($ model-> appointment_date ) – Pawan 2015-01-21 13:26:17