0

I set the timezone in my Yii2 application as stated in the docs in config/web.php:

'timeZone' => 'Europe/Berlin',

But: this is only valid for Yii2 and obviously not for PHP functions e.g. date(). The PHP timezone settings remain unchanged so there is a difference between PHP and Yii2.

How can I make the Yii2 timezone the leading timezone which sets the PHP timezone according to the timezone settings in Yii2 configuration?

EDIT

The timezone is set in the application component \Yii::$app, not in the formatter component.

I cannot modify php.ini file.

WeSee
  • 3,158
  • 2
  • 30
  • 58
  • i think you have to modify your php.ini [https://stackoverflow.com/questions/32224547/setting-the-timezone-for-php-in-the-php-ini-file](https://stackoverflow.com/questions/32224547/setting-the-timezone-for-php-in-the-php-ini-file) – Sfili_81 Feb 08 '19 at 09:04
  • Could ```date_default_timezone_set()``` help? How? – WeSee Feb 08 '19 at 09:07
  • `timeZone` property is provided as an alternative way of setting the default time zone of the PHP runtime. By configuring this property, you are essentially calling the PHP function `date_default_timezone_set()`. Do you have the latest information in the time zone database installed on your system?. You may refer to the [ICU manual](http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data) for details on updating the time zone database. – Muhammad Omer Aslam Feb 08 '19 at 10:26
  • Also,when formatting date and time values, Yii will convert them to the target time zone. The value being formatted is assumed to be in `UTC`, unless a time zone is explicitly given or you have configured [`yii\i18n\Formatter::$defaultTimeZone`](https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#$defaultTimeZone-detail). – Muhammad Omer Aslam Feb 08 '19 at 10:28

3 Answers3

7

I found a solution without modifying php.ini:

In ./web/index.php and ./yii set PHP timezone according to Yii2 settings:

$application = Yii::createObject('yii\web\Application', [$config->web()]);

// make PHP use the same timezone as Yii2
date_default_timezone_set($application->timeZone); 

$application->run();

Please don't forget to modify the console command yii / yii.bat in the similar way.

WeSee
  • 3,158
  • 2
  • 30
  • 58
2

You set the timezone in Formatter component. It means timezone will be used only to output date with Formatter. If you want to set global timezone you should set it in Application config. For example:

[
    'id' => 'basic',
    'timeZone' => 'Europe/Berlin',
    // other parameters of the application
]
Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
-3

common/config/main.php

'timeZone' => 'Asia/Tashkent',