A minimal package to convert any model datetime fields from UTC to desired timezone.
brainlet-ali/laravel-convert-timezone is a Laravel package for a minimal package to convert any model datetime fields from utc to desired timezone..
It currently has 7 GitHub stars and 102 downloads on Packagist (latest version 3.0).
Install it with composer require brainlet-ali/laravel-convert-timezone.
Discover more Laravel packages by brainlet-ali
or browse all Laravel packages to compare alternatives.
Last updated
Seamlessly handle timezone conversions in your Laravel applications. This lightweight package automatically converts datetime fields to your users' local timezones without modifying your database, keeping everything in UTC while displaying dates and times in the correct timezone for each user.
You can install the package via composer:
composer require brainlet-ali/laravel-convert-timezone
You can publish the config file with:
php artisan vendor:publish --provider="Brainlet\LaravelConvertTimezone\TzServiceProvider" --tag="tz-config"
Working with timezones in web applications is challenging. Store times in UTC? Display in user's timezone? Filter by local dates? This package solves these problems elegantly:
use Brainlet\LaravelConvertTimezone\Traits\ConvertTZ;
class Post extends Model
{
use ConvertTZ;
}
// That's it! All datetime fields now automatically convert to your configured timezone
$post = Post::first();
echo $post->created_at; // 2024-01-15 09:30:00 (in America/New_York instead of UTC)
Set your default timezone in the config file or .env:
// config/tz.php
'timezone' => env('TIMEZONE', 'America/New_York'),
Once you add the ConvertTZ trait, all datetime fields (created_at, updated_at, etc.) are automatically converted:
$user = User::create(['email' => '[email protected]']);
echo $user->created_at; // Displays in your configured timezone, not UTC
The trait respects existing accessors - if you have a custom accessor for a datetime field, it won't interfere:
public function getCreatedAtAttribute($value)
{
// Your custom logic here
return $value; // This will take precedence over timezone conversion
}
For more detailed usage examples and advanced features, see the full documentation.
composer test
Contributions are welcome! Please feel free to submit a Pull Request.
When creating an issue, please provide:
If you discover any security vulnerabilities, please email [email protected]
The MIT License (MIT). Please see License File for more information.