php.ini 中的时区是 UTC。系统时区是UTC。 yii defaultTimeZone 是 UTC。但是我的日期时间属性在保存到数据库之前被转换为我的时区“亚洲/加尔各答”。
例如:UTC 时间 12:00Hrs
我的输入 17.30hrs
我在 db 中期望的是 12:00 小时,而在 View 中是 17.30 小时
但是我在 db 中得到的是 17:30hrs,而我得到了 23:00hrs。
网页.php:
'formatter' =>
[
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat' => 'php:d-m-Y H:i a',
'timeFormat' => 'php:H:i A',
'timeZone' => 'Asia/Kolkata',
],
最佳答案
您可以选择使用预定义格式保存特定时间戳值。因此,让我们假设您已将后端中的日期时间字段定义为整数,并且您想将其保存为整数。您可以像这样设置行为
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',
ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
],
'value' => function() { return date('U'); // unix timestamp },
],
];
}
您可以配置 yii\i18n\formatter控制您的全局日期格式以显示您的语言环境。您可以在您可以访问的配置文件中设置类似的内容
'formatter' =>
[
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat' => 'php:d-m-Y H:i a',
'timeFormat' => 'php:H:i A',
'defaultTimeZone' OR 'timeZone' => 'Asia/Calcutta', //global date formats for display for your locale.
],
阅读本文 Link并引用 Doc .
希望它的作品。
关于yii2 - yii2 时区格式化程序中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139434/