where to set timezone property ?

I am using Advanced Application Template and I cant find where to put the timezone property which is described @ http://www.yiiframework.com/doc-2.0/guide-structure-applications.html#timezone

in php.ini, i set timezone to America/Edmonton, now I try to set timezone to America/Los_angeles

I try common/config/main.php => give me ReflectionException

I try common/config/params.php => does not change time zone to America/Los_angeles

Hey!

I’m not sure about advanced template but have you tried frontend and backend main config (for testing?)

In basic app the correct file is:

app/config/web.php




...

...

'sourceLanguage' => 'en',

'language' => 'de',

...

//'timeZone' => 'Europe/Berlin', // this is my default

'timeZone' => 'America/Los_Angeles', 

...

...



Now I put in any view:




echo date_default_timezone_get();



And I can see that the timezone changed.

EDIT:

Just tested also in advanced.

Works for me in every config…

For example my common/config/main.php




return [

    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',

    'timeZone' => 'Europe/Berlin',

    'components' => [

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

    ],


];



Regards

you are the man :)

I placed the ‘timeZone’ inside ‘components’ so it didnt work at first but follow your example fixes my issue.

Now, each user can set a timezone, let 's say I store timezone in a User table. how do I retrieve it and set timezone when user sign in ?




// in common/config/main.php


return [

    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',

    'timeZone' => User::findOne(Yii::$app->user->id)->timezone, // my guessing but it doesnt work <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />

    'components' => [

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

    ],


];



You’re welcome. ;)

Rest is easy.

You can access / change timeZone like everything in your config… ;)

In basic app in any view/action for example like this:




echo Yii::$app->timeZone; // timezone from config.

Yii::$app->timeZone = 'America/Los_Angeles'; // change timezone on the fly

echo Yii::$app->timeZone; // new timezone 



Means:

You have to load the user timezone in the class where you authenticate the user. ;)

You could do it in your actionLogin or in the User Model which implements the IdentityInterface.

(in basic its app/models/User.php and app/controllers/SiteController/actionLogin)

Regards

is there an entry place where I set timezone there and it will affect all pages ? Your method requires to set timezone for each page. lots of duplication.

I’m setting the timezone for the formatter component itself in config. In case of authenticated user I set timezone chosen by user and saved in db. This of course works if you use formatter to display dates and so on.




'formatter' => [

    'class'    => 'yii\i18n\Formatter',

    'timeZone' => Yii::$app->user->isGuest ? 'UTC' : Yii::$app->user->identity->timezone,

],



Thanks !

setting


    //Set the Timezone

'timezone' => 'Africa/Douala',

changed the timezone for me !

Thank you

‘timezone’ => ‘Africa/Douala’, it work
see more timezone https://www.php.net/manual/en/timezones.php