Laravel set timezone based on user preference

23 Jun, 2020

If you wonder how to set timzeone on Laravel based on user's preference, this post may be helpful for you. In this post, I will show you how to change the application timezone based on what user-defined.

I will add a column called timezone on user's table to store the logged in user's timezone.

Imagine that, we have two users those have settled following timezone.

  • User A - Timezone (Asia/Dhaka)
  • User B - Timezone (Asia/Kuala_Lumpur)

Now, I will create a new middleware called TimeZone.

php artisan make:middleware TimeZone

Now, define timezone middleware as follows-


namespace App\Http\Middleware;

use Closure;

class TimeZone
     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
    public function handle($request, Closure $next)

        return $next($request);

Now, let's register the middleware in the web middlewareGroup in App\Http\Kernel.php file.

protected $middlewareGroups = [
        'web' => [
            // \Illuminate\Session\Middleware\AuthenticateSession::class,

Now, if you store user's timezone, the entire application will follow whatever timezone user defines.

Note: Please take a note that, this solution is only for logged-in user. If you need to apply on the guest as well, you need to adjust your middleware based on your preference.

Thank you. :)

Hello. I’m Thouhedul Islam. Some people also know me as Suchi.

I am a Full-stack Developer. I love to play with PHP especially Laravel, JavaScript, MySql, and the latest web technology. Currently, I am working for GLEIF as my day time job.

At night I love to solve problems for the community. I love to be active at Laracasts.