jeevan628's avatar

Timezone config change on the fly

Hi all,

I tried to change the display timezone on the fly. My config is default to Toronto time in my controller I specify

Config::set("app.timezone","Europe/London");
var_dump(\Carbon\Carbon::now());

But the var_dump timezone still show "America/Toronto" time.

Does anyone know the issue. Thank you

0 likes
8 replies
bimalshah72's avatar

@jeevan628

This works me perfectly

 \App::setLocale('Europe/London');
var_dump(\Carbon\Carbon::now());
1 like
jeevan628's avatar

@taijuten That thread's quite long and more complex than what I'm trying to achieve. I just simply wanna change timezone display for a request. I thought doing

Config::set("app.timezone","Europe/London");

would help me convert all the datetime to my new timezone.

@bimalshah72 That's weird. It didn't work for me. As far as I know, setLocale is used for language setting(Ex: en,es,fr).

Thank guys

mikebronner's avatar

I think a few more questions will get you to a better use-case.

What is the real reason you want to change your timezone? Most likely it is to accommodate a user in a different timezone. In that case you will want to start each user's timezone in their profile (or user table, whichever you have implemented), then wherever you display days, you can adjust it in the view, maybe something like this:

My time: {{{ $date->setTimezone(Auth::user()->timezone)->format('d M Y G:i:s') }}}

I would (almost) NEVER change the timezone of the app, as it can wreak havoc on your database, if you save under different timezones. Adjusting the view to fit your needs is probably the better way to go, even if you adjust it in a variable you set in the controller (independent of the user).

jeevan628's avatar

I have 2 webservers: 1 in Toronto and the other in London. Database is in Toronto and all datetime attributes are saved in Toronto Time. But for UK, I want to display (just for display) the time in UK time. I tried to change the timezone in the index and show methods of each controller but It didn't work.

mikebronner's avatar

A good way to account for that is to set both servers to UTC time, then adjust on a per-user basis, as described above -- or if that doesn't fit your use-case, introduce a second timezone configuration parameter in the same config file, and maybe call it timezone_display, or maybe even an ENV variable, and use that to adjust your dates in the views. You will also likely need to adjust each displayed date individually, as it is reading the date from the database. I don't believe Laravel is adjusting the timezone when it loads data.

Also, see this thread: http://stackoverflow.com/questions/23833778/how-can-i-change-the-timezone-of-the-outputted-date-in-laravel-4

jeevan628's avatar

I ended up with

   if ($_SERVER['REQUEST_METHOD'] == "GET") {
            date_default_timezone_set(\Config::get("app.display_timezone"));
        }

In the constructor of ApiController. This will fix the display time.

Thank you guys.

Please or to participate in this conversation.