我已将日期字段appointment_date配置为datetimemysql db中的字段.
在我的模型Appointment.php规则设置如下:
public function rules()
{
return [
[['appointment_date','weekdays'], 'safe'],
[['appointment_date'], 'date','format' => 'd-M-yyyy H:m'],
Run Code Online (Sandbox Code Playgroud)
并在我设置的组件下的web.php中
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
Run Code Online (Sandbox Code Playgroud)
在我看来,我试图格式化显示日期,如下所示:
[
Yii::$app->formatter->asDatetime('appointment_date')
],
Run Code Online (Sandbox Code Playgroud)
现在我收到了错误 -
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
)
Run Code Online (Sandbox Code Playgroud)
而存储在数据库中的日期如下:2015-01-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']
],
Run Code Online (Sandbox Code Playgroud)
我正确格式化日期.
我想知道我在使用中做错了什么
Yii::$app->formatter->asDatetime('appointment_date')
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 7
我认为这只是一个小错字.您将字符串传递给asDateTime需要该值的方法
Yii::$app->formatter->asDatetime('appointment_date')
Run Code Online (Sandbox Code Playgroud)
应该
Yii::$app->formatter->asDatetime($model->appointment_date)
Run Code Online (Sandbox Code Playgroud)
文档:http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asDatetime() -detail
好吧,就像那样简单,我需要使用
'appoinment_date:date'
Run Code Online (Sandbox Code Playgroud)
要么
'appointment_date:datetime'
Run Code Online (Sandbox Code Playgroud)
并在组件格式化程序中添加格式
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat'=>'php:d-M-Y H:i:s'
Run Code Online (Sandbox Code Playgroud)
现在它工作正常.
| 归档时间: |
|
| 查看次数: |
23345 次 |
| 最近记录: |