Setting a dynamic time zone in yii2

Does anyone know how to set a dynamic time zone for each user? If the time zone is stored in the database, how can I get it from db and set it at runtime, so I don't need to set it every time in my codes?

+6
source share
1 answer

This is an example of how to do this if the time zone is stored as a row in the timezone column in the users table. Add this to your application configuration:

 'on beforeRequest' => function () { $user = Yii::$app->user->identity; if ($user && $user->timezone) { Yii::$app->setTimeZone($user->timezone); } }, 

This code is run before the request and sets the time zone depending on the specific user. Of course, you can move it to a separate class and call it here.

Official documents:

+9
source

All Articles