snscripts/timezones
最新稳定版本:3.0.0
Composer 安装命令:
composer require snscripts/timezones
包简介
Timezone helpers to convert to and from UTC with Laravel Blade directives
README 文档
README
Introduction
Timezones is a PSR-2 compliant package used for easily converting dates into UTC or into your local timezones. Bundled is also support for Laravel Facades and blade directives to make usage even easier!
Installation
Composer
Simply require the package via composer into your app.
composer require mbarlow/timezones 2.*
Setup
To get started simply instantiate the Timezones object with the new keyword in PHP.
$timezones = new \MBarlow\Timezones\Timezones;
Laravel
Bundled with this package is a Laravel Service provider that is setup for auto-discovery.
If you are not using Laravel 5.5 then you can add the following Service Provider to your config/app.php config.
[
/*
* Package Service Providers...
*/
\MBarlow\Timezones\TimezonesServiceProvider::class,
];
The service provider will bind the Timezones Object to the container allowing you to type hint in the object.
use MBarlow\Timezones\Timezones; class MyController { public function myControllerAction(Timezones $timezones) { // } }
Usage
To use, first instantiate the object or typehint the object into your method.
convertToUTC
$timezones = new \MBarlow\Timezones\Timezones; $dateTime = '2020-08-14 12:23:00'; $tz = 'America/New_York'; $format = 'Y-m-d H:i:s'; $timezones->convertToUTC($dateTime, $tz, $format);
use MBarlow\Timezones\Timezones; class MyController { public function myControllerAction(Timezones $timezones) { $dateTime = '2020-08-14 12:23:00'; $tz = 'America/New_York'; $format = 'Y-m-d H:i:s'; $timezones->convertToUTC($dateTime, $tz, $format); } }
This method should be used when parsing an user inputted date to convert it to UTC ready for storing in your database.
$dateTimeis a required field and can either be an instance of \DateTime that was created with the correct local timezone or it can be a string which will be passed into an instance of \DateTime.
If $dateTime is an instance of \DateTime then $tz is not required and should default to null. If $dateTime is passed through as a string then $tz should either be an instance of \DateTimeZone or a string describing a valid timezone.
$format is an optional field that defaults to standard MySQL datetime format of YYYY-MM-DD HH:MM:SS.
convertToLocal
$timezones = new \MBarlow\Timezones\Timezones; $dateTime = '2020-08-14 12:23:00'; $tz = 'America/New_York'; $format = 'jS F Y H:i'; $timezones->convertToLocal($dateTime, $tz, $format);
use MBarlow\Timezones\Timezones; class MyController { public function myControllerAction(Timezones $timezones) { $dateTime = '2020-08-14 12:23:00'; $tz = 'America/New_York'; $format = 'jS F Y H:i'; $timezones->convertToLocal($dateTime, $tz, $format); } }
This method should be used in your "views or in your API endpoints" to transform a UTC date stored within your database into the local timezone for the user viewing the page or requesting the data.
$dateTime is a required field and can either be an instance of \DateTime that was created with the correct UTC timezone or it can be a string which will be passed into an instance of \DateTime.
$tz is a required field and can either be an instance of \DateTimeZone or a string describing a valid timezone and should be the timezone of the end user.
$format is an optional field that defaults to standard MySQL datetime format of YYYY-MM-DD HH:MM:SS.
timezoneList
This method can be used for API endpoints or within your templates to generate an array of formatted timezones. The generated array will return an associative array with the timezone as the key and a nice label with offset as the value.
$timezones = new \MBarlow\Timezones\Timezones; print_r($timezones->timezoneList()); /* Array ( [Pacific/Midway] => (UTC -11:00) Midway [Pacific/Niue] => (UTC -11:00) Niue [Pacific/Pago_Pago] => (UTC -11:00) Pago Pago [Pacific/Honolulu] => (UTC -10:00) Honolulu [Pacific/Rarotonga] => (UTC -10:00) Rarotonga [Pacific/Tahiti] => (UTC -10:00) Tahiti [Pacific/Marquesas] => (UTC -09:30) Marquesas [America/Adak] => (UTC -09:00) Adak [Pacific/Gambier] => (UTC -09:00) Gambier [America/Anchorage] => (UTC -08:00) Anchorage [America/Juneau] => (UTC -08:00) Juneau [America/Metlakatla] => (UTC -08:00) Metlakatla [America/Nome] => (UTC -08:00) Nome [Pacific/Pitcairn] => (UTC -08:00) Pitcairn [America/Sitka] => (UTC -08:00) Sitka .... ) */
$timezones = new \MBarlow\Timezones\Timezones; $list = $timezones->timezoneList(); echo '<select="timezone">'; foreach ($list as $timezone => $label) { echo '<option value="' . $timezone . '">' . $label . '</option>'; } echo '</select>';
Laravel Extras
If you have installed the package into a Laravel application a few extra goodies are available.
Facade
Should you prefer to use a Facade, one has been bundled and made available with the Service Provider. The facade is available at `\Timezones'.
Both methods described in the "Usage" section above are available.
\Timezones::convertToUTC();
\Timezones::convertToLocal();
Blade Directive
A blade directive will also be registered by the Service Provider. This only supports the convertToLocal method and is designed for use within your blade templates to convert a UTC datetime into the end users time and display on the page in the correct format.
@displayDate($dateTime, $tz, $format)
All parameters match those defined for the convertToLocal method however all fields are required when using the blade directive.
Testing
If you wish to run the tests, clone out the repository
git clone git@github.com:mikebarlow/Timezones.git
Change to the root of the repository and run composer install with the dev dependencies
cd Timezones
composer install
A script is defined in the composer.json to run both the code sniffer and the unit tests
composer run test
Or run them individually as required
./vendor/bin/phpunit
./vendor/bin/phpcs --standard=PSR2 --ignore=src/Facade/* src
Changelog
You can view the changelog HERE
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
snscripts/timezones 适用场景与选型建议
snscripts/timezones 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 502 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「helpers」 「conversion」 「laravel」 「timezone」 「datetime」 「utc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 snscripts/timezones 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 snscripts/timezones 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 snscripts/timezones 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Fixer.io data provider for Peso
HTML and form generation
Falsy helps you manage half-truths with PHP
Color conversion library
Convert and serve jpeg/png to webp with PHP (if at all possible)
统计信息
- 总下载量: 502
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-14