Jump to content

Change timezone per user in admin


gebeer
 Share

Recommended Posts

Hello,

my site has users from different timezones logging in to the backend.

I have a "timezone" field for each user where I store their timezone string.

Now I need to show dates in the backend in the user's timezone.

I know I can set timezone for php date calculations with: date_default_timezone_set("Europe/Berlin") for example.

In config.php I have $config->timezone = 'America/Phoenix'; as a general setting for that site.

Now when a user logs on I want to set the timezone for his session to date_default_timezone_set($user->timezone).

Where would be the best place to set this?

Link to comment
Share on other sites

Because it depends on login / logout, my first thought is:

$this->pages->addHookAfter('Session::logout', $this, 'yourFunction');
$this->pages->addHookAfter('Session::login', $this, 'yourFunction');

If this doesn't fit well, there is also

$this->pages->addHookBefore('ProcessLogin::execute', $this, 'yourFunction');
$this->pages->addHookBefore('ProcessLogin::executeLogout', $this, 'yourFunction');

// or 

$this->pages->addHookAfter('ProcessLogin::execute', $this, 'yourFunction');
$this->pages->addHookAfter('ProcessLogin::executeLogout', $this, 'yourFunction');
  • Like 1
Link to comment
Share on other sites

Thank you Horst,

I will try those hooks.

Do I then have to set the timezone with $config->timezone = $user->timezone

or with

date_default_timezone_set($user->timezone)

Just asking because I'm not sure whether PW will override the date_default_timezone_set($user->timezone) with $config->timezone value from config.php.

Link to comment
Share on other sites

I'm not very familiar with (user) timezones. A quick search through PWs wire files shows that timezone is used in ProcessWire::config with

ini_set('date.timezone', $config->timezone);

So I would change $config->timezone and PHPs date.timezone. But haven't worked / tested with that!

In regard of getting the original / default value and not a runtime-changed value from $config, there is this possible workaround:

function getDefaultValuefromConfig($key) {
    $config = new stdClass();
    include( wire-config.php );
    include( site-config.php );
    // now we have merged default settings of $config in local scope of this function 
    // and can return the requested one
    return $config->$key;
}
  • Like 1
Link to comment
Share on other sites

I'm not sure if setting the timezone for php is really the most sane way to go about it. I'd rather keep php's timezone to be consistent and just use a library like carbon to handle the timezone differences for the frontend.

Edit: Just read that you need it for the backend as well, so that's not so much an option.

  • Like 4
Link to comment
Share on other sites

I would add a field to the user template

and set its timezone for every user page.

as you did.

then

using horst´s example hooks and session

public function init() {
    $this->pages->addHookAfter('Session::logout', $this, 'timezoneLogout');
    $this->pages->addHookAfter('Session::login', $this, 'timezoneLogin');
}

public function timezoneLogin(HookEvent $event) {
    $user = $event->object;
    $session = wire('session');
    $session->timezone = $user->timezone;
}

public function timezoneLogout(HookEvent $event) {
    $session = wire('session');
    $session->remove('timezone');
}

Now you can use the session var across the backend and the frontend without querying the user :D

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...