Dynamically changing TimeZone in laravel

My project has time zone expansion (PT, CST, etc.), when Admin changes the time zone from the drop-down list, the admin panel reflects the time zone from the selected drop-down list.
How to change the "time zone" Config / app.php (Application Timezone) according to the selected option.

+5
source share
4 answers

You can use the Laravel config helper function to set the time zone. However, this will only affect the request you receive.

 config(['app.timezone' => $timezone]); 

If your goal is to change once with the time zone and run each query, then how about saving the changed time zone to the database or file. Then write a query for the database or read the file in app / config.php and change the index timeline value in the file.

For example (example file):

When you change the time zone, it saves the file.

 file_put_contents("path/to/file", $timezone); 

And, in app/config.php

 $timezone= file_get_contents("path/to/file"); return [ . . . 'timezone' => $timezone, . . . ] 
+2
source

you also need to call date_default_timezone_set

  config(['app.timezone' => $timezone]); date_default_timezone_set($timezone); 
+3
source

If you want to save a new time zone for all future requests, you need to use a package like larapack / config-writer so that it can save the time zone in the app configuration file.

Another way to handle this is to save the time zone in the database, retrieve it in each query and set it dynamically with config(['app.timezone' => $timezone]) .

+2
source

You can use middleware to achieve this, depending on which routes you write, list all those who use this middleware.
You can get this data from the database and apply it as described below.

 config('app.timezone', 'your selected timezone') 
+2
source

Source: https://habr.com/ru/post/1274263/


All Articles