Looking to hire Laravel developers? Try LaraJobs

laravel-user-timezone maintained by athwari

Description
Per-user timezone management for Laravel applications.
Author
Last update
2026/06/30 01:32 (dev-main)
License
Downloads
0

Comments
comments powered by Disqus

Per-user timezone management for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Store dates in UTC, display in each user's preferred timezone.

Installation

composer require athwari/laravel-user-timezone

Publish the config and migration:

php artisan vendor:publish --tag=user-timezone-config
php artisan vendor:publish --tag=user-timezone-migrations
php artisan migrate

Configuration

// config/user-timezone.php
return [
    'user_model' => env('USER_TIMEZONE_MODEL', 'App\\Models\\User'),
    'column' => env('USER_TIMEZONE_COLUMN', 'timezone'),
    'fallback' => env('USER_TIMEZONE_FALLBACK', config('app.timezone')),
    'middleware' => [
        'enabled' => env('USER_TIMEZONE_MIDDLEWARE_ENABLED', true),
        'set_php_timezone' => env('USER_TIMEZONE_SET_PHP_TIMEZONE', true),
    ],
];

Usage

Facade

use Athwari\LaravelUserTimezone\Facades\UserTimezone;

UserTimezone::get();                          // 'Europe/London'
UserTimezone::resolveFromUser($user);         // 'Europe/London' or fallback
UserTimezone::set('America/New_York');        // override for request
UserTimezone::clear();                        // remove override
UserTimezone::now();                          // Carbon in user's timezone
UserTimezone::convert($post->created_at);     // Carbon converted to user's timezone
UserTimezone::isValid('Invalid/Zone');        // false

Helper

user_timezone(); // string

Middleware

The ApplyUserTimezone middleware is registered automatically. It resolves the user's timezone and sets both app.timezone and PHP's default timezone for the request lifecycle.

Model Trait

Add HasTimezone to your User model:

use Athwari\LaravelUserTimezone\Traits\HasTimezone;

class User extends Authenticatable
{
    use HasTimezone;
}
$user->getTimezone();              // 'Europe/London' or fallback
$user->setTimezone('Asia/Tokyo');  // validates and sets
$user->timezone = 'UTC';           // validated attribute mutator

Opt-in Date Conversion

For models where you want explicit timezone-aware accessors:

use Athwari\LaravelUserTimezone\Traits\ConvertsDatesToUserTimezone;

class Post extends Model
{
    use ConvertsDatesToUserTimezone;
}
$post->inUserTimezone('created_at'); // Carbon in user's timezone

Testing

composer test

Changelog

Please see CHANGELOG for details.

License

MIT