SVM
SVM

Reputation: 433

Change TimeZone dynamically in laravel

In my project there is drop-down of time zones(PT,CST etc), when Admin changes the timezone from drop-down the admin panel reflects the timezone from the selected drop down.
How to change Config/app.php "timezone"( Application Timezone) according to the selected option.

Upvotes: 19

Views: 17348

Answers (7)

Andrew Fox
Andrew Fox

Reputation: 890

I have an older Laravel 5.4 application where there are multiple company locations and each of the users are assigned to one of those locations. The timezone is set on the company location in the database. I tried to use @PS324 solution, but I'm accessing the location through the Auth::user(), which isn't available in the AppServiceProvider. What I did was do what @Rahul suggested and create a middleware, and I used the code that @PS324 provided.

Here's what I ended up with and it works a treat:

  1. Create middleware "SetTimezone"

     if (Auth::check()) {
         $user = Auth::user();
    
         if ($user->base_location && $user->base_location->timezone) {
             date_default_timezone_set($user->base_location->timezone);
         }
     }
    
  2. Register it in App\Http\Kernel.php (\App\Http\Middleware\SetTimezone::class)

I ran {{ \Carbon\Carbon::now()->format('H:i:s') }} in my blade view to test it and it's showing the correct timezone.

I hope that helps!

Upvotes: 0

PS324
PS324

Reputation: 11

In AppServiceProvider boot()

$settings = Settings::first();
date_default_timezone_set($settings->timezone ? $settings->timezone : config('app.timezone'));

This overrides default TZ value in app.php

Upvotes: 1

kashef_php
kashef_php

Reputation: 60

You can use service provider. That way, you first need to get the settings information from the database and then change the settings inside the config file.

Config::set('app.timezone', $db['timezone'] ?? config('app.timezone'));

Upvotes: 1

Phoenix404
Phoenix404

Reputation: 1058

You can use Laravel helper function config to set timezone. However, this will affects only the request you will receive.

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

If your goal is to change once the timezone and run on every request then what about saving the changed timezone in DB or in a file. Then, write a query for DB or read a file in app/config.php and change the value of index timezone in a file.

For example (file example):

When you changed the timezone it saves in a file.

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

And, in app/config.php

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

Upvotes: 11

the_hasanov
the_hasanov

Reputation: 843

you need also call date_default_timezone_set

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

Upvotes: 20

Rahul
Rahul

Reputation: 18567

You can use middleware to achieve this, whichever routes you will write mention all those applying with that middleware.
You can fetch that data from database and apply it like as follows,

config('app.timezone', 'your selected timezone')

Upvotes: 3

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

If you want to keep a new time zone for all future requests, you need to use a package like larapack/config-writer to be able to save the time zone to the app config file.

Another way to handle this is to keep time zone in DB, fetch it in every request and set it with config(['app.timezone' => $timezone]) dynamically.

Upvotes: 3

Related Questions