Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikebarwick's avatar

User Specified Timezones - config?

What's the trick here guys? I want to set this globally for each user. What's the best approach? In user table I have timezone column (default, 'America/New_York, etc.).

Speak to me, wise ones...show me the light.

0 likes
9 replies
SachinAgarwal's avatar

@mikebarwick You can add a middleware for setting the timezone for every request with

Config::set('app.timezone','custom time zone');

Its better store user specified the time zone in session and set it in middleware for every request.

usman's avatar

@mikebarwick setting the app.timezone config key inside a middleware won't help, because by the time a request will reach the middleware Laravel would have already utilized this key. You will need to use the date_default_timezone_set() function.

You can create a separate middleware or you can just utilize the Authenticate middleware with the following changes:

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('login');
            }
        }
        date_default_timezone_set($this->auth->user()->timezone);
        return $next($request);
    }

Just my two cents :)

Update://

My bad, using Authenticate middleware will only set the timezone for authenticated routes. You will need a separate middleware.

Usman

Snapey's avatar

@usman I think you were right in the first place... you can't set a user' time zone unless you know who they are, which probably means they will have to be authenticated.

If not authenticated, then the other possibility is to give the user a cookie when they do login and set their preferred time zone. This cookie could then be examined at each session start. A bit like Amazon knows who I am when I first hit the site but then I have to login to do anything with my account.

usman's avatar
usman
Best Answer
Level 27

@Snapey thanks for the insight :) I think the following middleware is a little better, it is based on your suggestions:

//file: app/Http/Middleware/TimeZone.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class TimeZone {
    
    /**
     * The current logged in user instance
     * @var [type]
     */
    protected $user;

    /**
     * creates an instance of the middleware
     * @param Guard $auth
     */
    public function __construct(Guard $auth)
    {
        $this->user = $auth->user();
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {   
        $this->setTimeZone($request);
        return $this->addTimeZoneCookie($request, $next($request));
    }

    /**
     * sets the time zone from cookie or from the user setting
     * @param Illuminate\Http\Request $request
     */
    public function setTimeZone($request)
    {
        if($this->user)
        {
            return date_default_timezone_set($this->user->timezone);
        }
    
        $timeZone = $request->cookie('time_zone');

        if($timeZone)
        {
            return date_default_timezone_set($timeZone);
        }
        return;
    }

    /**
     * adds the cookie to response
     * @param Illuminate\Http\Request $request
     * @param Illuminate\Http\Response $response
     */
    public function addTimeZoneCookie($request, $response)
    {
        if(! $request->cookie('time_zone') && ! is_null($this->user))
        {
            return $response->withCookie(cookie('time_zone', $this->user->timezone, 120));
        }
        return $response;
    }
}

/cc : @mikebarwick

Usman.

3 likes
mikebarwick's avatar

@usman Thanks man - and everyone else!

Question - is this the best practice though? the more I think about it. What happens if the user changes timezones? In what I'm building, a company can have many users (that can assign tasks to everyone). What happens if person A in Toronto sets a task for 3pm, but person B (who the task is assigned to) lives in California?

Snapey's avatar

You usually have to store all such times in UTC and then present them in local time according to the logged-in user.

The main issue with this approach is how to handle things like 'today's events'. The definition of today varies depending on where you are.

Braunson's avatar

Great middleware here, been looking for something like this :D

nikocraft's avatar

I am making an app where user can publish an article. If somebody in Japan publishes an article on 2017/03/25 09:00 should I set the articles published date and time to that time or should I set it to servers time?

Also if somebody in USA enters the website and is about to read the article, what date should the article say it was published?

If I say 2017/03/25 09:00 that time has not happend yet for the user in the USA and it would be in future for him, what time should I show to somebody from the other end of the world?

Please or to participate in this conversation.