Does anyone know how to set a dynamic timezone for each user? If the timezone is stored in database, how do i get it from db and set it at runtime so that i dont need to set it everytime in my codes?
This is an example how to do it assuming timezone is stored as string in column timezone
in users
table. Add this to your application config:
'on beforeRequest' => function () {
$user = Yii::$app->user->identity;
if ($user && $user->timezone) {
Yii::$app->setTimeZone($user->timezone);
}
},
This code will run before request and set timezone depending on specific user. Of course you can move it to separate class and call it from here.
Official docs:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With