jamesmills/laravel-timezone
Composer 安装命令:
composer require jamesmills/laravel-timezone
包简介
Timezone storage and retrieval for Laravel
README 文档
README
An easy way to set a timezone for a user in your application and then show date/times to them in their local timezone.
How does it work
This package listens for the \Illuminate\Auth\Events\Login event and will then automatically set a timezone on your user model (stored in the database).
This package uses the torann/geoip package which looks up the users location based on their IP address. The package also returns information like the users currency and users timezone. You can configure this package separately if you require.
How to use
You can show dates to your user in their timezone by using
{{ Timezone::convertToLocal($post->created_at) }}
Or use our nice blade directive
@displayDate($post->created_at)
Installation
Pull in the package using Composer
composer require jamesmills/laravel-timezone
Publish database migrations
php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=migrations
Run the database migrations. This will add a timezone column to your users table.
php artisan migrate
Examples
Showing date/time to the user in their timezone
Default will use the format jS F Y g:i:a and will not show the timezone
{{ Timezone::convertToLocal($post->created_at) }}
// 4th July 2018 3:32:am
If you wish you can set a custom format and also include a nice version of the timezone
{{ Timezone::convertToLocal($post->created_at, 'Y-m-d g:i', true) }}
// 2018-07-04 3:32 New York, America
Using blade directive
Making your life easier one small step at a time
@displayDate($post->created_at) // 4th July 2018 3:32:am
And with custom formatting
@displayDate($post->created_at, 'Y-m-d g:i', true) // 2018-07-04 3:32 New York, America
Saving the users input to the database in UTC
This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance.
$post = Post::create([ 'publish_at' => Timezone::convertFromLocal($request->get('publish_at')), 'description' => $request->input('description'), ]);
Custom Configuration
Publishing the config file is optional.
php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=config
Flash Messages
When the timezone has been set, we display a flash message, By default, is configured to use Laravel default flash messaging, here are some of the optional integrations.
laracasts/flash - 'flash' => 'laracasts'
mercuryseries/flashy - 'flash' => 'mercuryseries'
spatie/laravel-flash - 'flash' => 'spatie'
mckenziearts/laravel-notify - 'flash' => 'mckenziearts'
usernotnull/tall-toasts - 'flash' => 'tall-toasts'
To override this configuration, you just need to change the flash property inside the configuration file config/timezone.php for the desired package. You can disable flash messages by setting 'flash' => 'off'.
Overwrite existing timezones in the database
By default, the timezone will be overwritten at each login with the current user timezone. This behavior can be restricted to only update the timezone if it is blank by setting the 'overwrite' => false, config option.
Default Format
By default, the date format will be jS F Y g:i:a. To override this configuration, you just need to change the format property inside the configuration file config/timezone.php for the desired format.
Lookup Array
This lookup array configuration makes it possible to find the remote address of the user in any attribute inside the Laravel request helper, by any key. Having in mind when the key is found inside the attribute, that key will be used. By default, we use the server attribute with the key REMOTE_ADDR. To override this configuration, you just need to change the lookup property inside the configuration file config/timezone.php for the desired lookup.
User Message
You may configure the message shown to the user when the timezone is set by changing the message property inside the configuration file config/timezone.php
Underlying GeoIp Package
If you wish to customise the underlying torann/geoip package you can publish the config file by using the command below.
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
This package is 100% free and open-source, under the MIT license. Use it however you want.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Issues
If you receive a message like This cache store does not support tagging this is because the torann/geoip package requires a caching driver which supports tagging and you probably have your application set to use the file cache driver. You can publish the config file for the torann/geoip package and set 'cache_tags' => null, to solve this. Read more about this issue here.
jamesmills/laravel-timezone 适用场景与选型建议
jamesmills/laravel-timezone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 790.61k 次下载、GitHub Stars 达 696, 最近一次更新时间为 2018 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「timezone」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jamesmills/laravel-timezone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jamesmills/laravel-timezone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jamesmills/laravel-timezone 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Datetime helpers for timezone handling
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Official PHP SDK for the IPGeolocation.io IP Location API with single and bulk lookup support.
Geonames importer and models for Eloquent ORM. This repo is a fork from Yurtesen/Geonames.
Alfabank REST API integration
Simple TimeZone PHP library (based on Scott Connerly timezone library).
统计信息
- 总下载量: 790.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 698
- 点击次数: 20
- 依赖项目数: 14
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-30