brainlet-ali/laravel-convert-timezone
Composer 安装命令:
composer require brainlet-ali/laravel-convert-timezone
包简介
A minimal package to convert any model datetime fields from UTC to desired timezone.
README 文档
README
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.
Installation
Laravel
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"
Why Use This Package?
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:
- ✅ Zero Database Changes - Keep storing everything in UTC
- ✅ Automatic Conversion - DateTime fields automatically display in the configured timezone
- ✅ Smart Filtering - Query by dates/times in any timezone without complex calculations
- ✅ Laravel Native - Works seamlessly with Eloquent models and query builder
- ✅ Minimal Setup - Just add a trait to your model and you're ready to go
Quick Start
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)
Basic Usage
Configuration
Set your default timezone in the config file or .env:
// config/tz.php 'timezone' => env('TIMEZONE', 'America/New_York'),
Automatic Conversion
Once you add the ConvertTZ trait, all datetime fields (created_at, updated_at, etc.) are automatically converted:
$user = User::create(['email' => 'user@example.com']); echo $user->created_at; // Displays in your configured timezone, not UTC
Accessor Compatibility
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 }
Documentation
For more detailed usage examples and advanced features, see the full documentation.
Requirements
- PHP 7.4+ or 8.0+
- Laravel 9.0+ | 10.0+ | 11.0+
- Doctrine DBAL 3.8+
Testing
composer test
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Reporting Issues
When creating an issue, please provide:
- Clear description of the problem
- Steps to reproduce the issue
- Expected vs actual behavior
- Laravel version and PHP version
- Any relevant error messages or stack traces
Security Issues
If you discover any security vulnerabilities, please email brainlet.ali@gmail.com
License
The MIT License (MIT). Please see License File for more information.
brainlet-ali/laravel-convert-timezone 适用场景与选型建议
brainlet-ali/laravel-convert-timezone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 102 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「brainlet-ali」 「laravel-convert-timezone」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 brainlet-ali/laravel-convert-timezone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brainlet-ali/laravel-convert-timezone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 102
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-23