jackiedo/timezonelist
Composer 安装命令:
composer require jackiedo/timezonelist
包简介
A small package use to create a timezone list box in Laravel
README 文档
README
Overview
- Laravel Timezone List
- Overview
- Feature
- Versions and compatibility
- Documentation
- Contributors
- License
Feature
- Render a timezone listbox (select element) in Laravel
- Render a timezone array in Laravel
Versions and compatibility
Currently, there are some branches of Timezone-List is compatible with the following version of Laravel framework
| Timezone-List branch | Laravel version |
|---|---|
| 4.x | 4.x |
| 5.x | 5.x and later |
This documentation is use for branch 5.x
Documentation
Installation
You can install this package through Composer with the following steps:
Step 1 - Require Package
At the root of your application directory, run the following command (in any terminal client):
$ composer require jackiedo/timezonelist
Note: Since Laravel 5.5, service providers and aliases are automatically registered. But if you are using Laravel 5.4 and earlier, you must register the Service Provider and the Facade manually. Do the following steps:
Step 2 - Register Service Provider
Open config/app.php, and add a new line to the providers section:
... Jackiedo\Timezonelist\TimezonelistServiceProvider::class,
Step 3 - Register Facade Alias
Add the following line to the aliases section in file config/app.php:
'Timezonelist' => Jackiedo\Timezonelist\Facades\Timezonelist::class,
Usage
Working With Facade
Laravel Timezone List has a facade with the fully qualified namespace is Jackiedo\Timezonelist\Facades\Timezonelist. You can perform all operations through this facade.
Example:
<?php namespace Your\Namespace; use Jackiedo\Timezonelist\Facades\Timezonelist; class YourClass { public function yourMethod() { $return = Timezonelist::doSomething(); } }
Note: If at the installation step, you have registered the Facde alias, then in the areas where the namespace is not used, eg views..., you can completely use that Facde alias to use instead of having to use the fully qualified Facde namespace.
Example: (use in the resources/views/demo.blade.php file)
<div class="form-group">
{!! Timezonelist::doSomething() !!}
</div>
Using As Regular Class
You can completely use the package through the Jackiedo\Timezonelist\Timezonelist class like using a regular object class.
Example:
namespace Your\Namespace; use Jackiedo\Timezonelist\Timezonelist; class YourClass { public function yourMethod() { $timezoneList = new Timezonelist; $return = $timezoneList->doSomething(); } }
Available Methods
Render a timezone listbox
Syntax:
/** * Create a select box of timezones. * * @param string $name The name of the select tag * @param null|string $selected The selected value * @param null|array|string $attr The HTML attributes of select thag * @param bool $htmlencode Use HTML entities for values of select tag * * @return string */ public function toSelectBox($name, $selected = null, $attr = null, $htmlencode = true); /** * Alias of the `toSelectBox()` method. * * @deprecated 6.0.0 This method name no longer matches the semantics */ public function create($name, $selected = null, $attr = null, $htmlencode = true);
Note: the
create()method will be removed in the version 6.x
Example:
echo Timezonelist::toSelectBox('timezone');
This will output the following HTML code:
<select name="timezone"> <optgroup label="General"> <option value="GMT">GMT timezone</option> <option value="UTC">UTC timezone</option> </optgroup> <optgroup label="Africa"> <option value="Africa/Abidjan">(GMT/UTC + 00:00) Abidjan</option> <option value="Africa/Accra">(GMT/UTC + 00:00) Accra</option> <option value="Africa/Addis_Ababa">(GMT/UTC + 03:00) Addis Ababa</option> <option value="Africa/Algiers">(GMT/UTC + 01:00) Algiers</option> <option value="Africa/Asmara">(GMT/UTC + 03:00) Asmara</option> <option value="Africa/Bamako">(GMT/UTC + 00:00) Bamako</option> <option value="Africa/Bangui">(GMT/UTC + 01:00) Bangui</option> <option value="Africa/Banjul">(GMT/UTC + 00:00) Banjul</option> <option value="Africa/Bissau">(GMT/UTC + 00:00) Bissau</option> ... </optgroup> <optgroup label="America"> <option value="America/Adak">(GMT/UTC - 10:00) Adak</option> <option value="America/Anchorage">(GMT/UTC - 09:00) Anchorage</option> <option value="America/Anguilla">(GMT/UTC - 04:00) Anguilla</option> <option value="America/Antigua">(GMT/UTC - 04:00) Antigua</option> <option value="America/Araguaina">(GMT/UTC - 03:00) Araguaina</option> <option value="America/Argentina/Buenos_Aires">(GMT/UTC - 03:00) Argentina/Buenos Aires</option> <option value="America/Argentina/Catamarca">(GMT/UTC - 03:00) Argentina/Catamarca</option> <option value="America/Argentina/Cordoba">(GMT/UTC - 03:00) Argentina/Cordoba</option> <option value="America/Argentina/Jujuy">(GMT/UTC - 03:00) Argentina/Jujuy</option> ... </optgroup> ... </select>
The
Timezonelist::toSelectBox()method has four parameters:
- The first parameter is required, it is the name attribute of the rendered select tag
- The second parameter use to set selected value of list box.
- The third parameter use to set HTML attribute of select tag.
- The fourth parameter allow to use some HTML entities in the rendered select tag. The purpose is to make the element look better.
Example:
// Render a select tag with the name `timezone` and the `Africa/Asmara` option preselected Timezonelist::toSelectBox('timezone', 'Africa/Asmara'); // Render tag with some HTML attributes Timezonelist::toSelectBox('timezone', null, [ 'id' => 'timezone', 'class' => 'styled', ... ]); // Or with other method Timezonelist::toSelectBox('timezone', null, 'id="timezone" class="styled"');
Example of the difference of the
fourth parameter
Render a timezone array
Syntax:
/** * Create a timezone array. * * @param bool $htmlencode Use HTML entities for items * * @return mixed */ public function toArray($htmlencode = true);
Example:
$timezoneList = Timezonelist::toArray(false); // The returned list will be // [ // "General" => [ // "GMT" => "(GMT/UTC + 00:00) GMT", // "UTC" => "(GMT/UTC + 00:00) UTC", // ], // "Africa" => [ // "Africa/Abidjan " => "(GMT/UTC + 00:00) Abidjan", // "Africa/Accra" => "(GMT/UTC + 00:00) Accra", // "Africa/Addis_Ababa" => "(GMT/UTC + 03:00) AddisAbaba", // "Africa/Algiers" => "(GMT/UTC + 01:00) Algiers", // "Africa/Asmara" => "(GMT/UTC + 03:00) Asmara", // ... // ], // "America" => [ // "America/Adak" => "(GMT/UTC - 09:00) Adak", // "America/Anchorage" => "(GMT/UTC - 08:00) Anchorage", // "America/Anguilla" => "(GMT/UTC - 04:00) Anguilla", // "America/Antigua" => "(GMT/UTC - 04:00) Antigua", // "America/Araguaina" => "(GMT/UTC - 03:00) Araguaina", // ... // ], // ... // ]
Filter the returned list
By default, the toSelectBox, toArray... methods will return a list of timezones consisting of 11 groups (one common group and 10 groups corresponding to the continents):
- General
- Africa
- America
- Antarctica
- Arctic
- Asia
- Atlantic
- Australia
- Europe
- Indian
- Pacific
In some cases, we don't want to get in that list some specified groups, we can do that by some of the following methods:
Get only some specified groups
Syntax:
/** * Set the filter of the groups want to get. * * @param array $groups * * @return $this */ public function onlyGroups($groups = []);
Example:
... $return = Timezonelist::onlyGroups(['Asia', 'America'])->toSelectBox('timezone');
Exclude some specified groups
Syntax:
/** * Set the filter of the groups do not want to get. * * @param array $groups * * @return $this */ public function excludeGroups($groups = []);
Example:
... $return = Timezonelist::excludeGroups(['General'])->toArray();
Change the layout of the returned list
In some cases, we need to change the form of the list that we will receive, we can do it through some of the following methods:
Decide whether to split group or not
Syntax:
/** * Decide whether to split group or not. * * @param bool $status * * @return $this */ public function splitGroup($status = true);
Example:
$return = Timezonelist::splitGroup(false)->excludeGroups(['General'])->toSelectBox('timezone');
Decide whether to show the timezone offset or not
Syntax:
/** * Decide whether to show the offset or not. * * @param bool $status * * @return $this */ public function showOffset($status = true);
Example:
$return = Timezonelist::showOffset(false)->excludeGroups(['General'])->toSelectBox('timezone');
Reset all config and return new list
Always keep in mind that, if we use package methods via Facade, we are using it as a static interface to a class. This means that the filter and layout settings will always be saved for the next call. If we don't want to reuse these settings, we have to execute the following method on the next call:
Syntax:
/** * Return new static to reset all config. * * @return $this */ public function reset();
Example:
// Genrate one select box, exclude two groups of timezones, Asia and Africa $selectBox = Timezonelist::excludeGroups(['Asia', 'Africa'])->toSelectBox('timezone'); $list1 = Timezonelist::toArray(); // Two groups, Asia and Africa, will not be loaded into the result $list2 = Timezonelist::reset()->toArray() // All groups will be loaded
Contributors
This project exists thanks to all its contributors.
License
MIT © Jackie Do
jackiedo/timezonelist 适用场景与选型建议
jackiedo/timezonelist 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 638.19k 次下载、GitHub Stars 达 110, 最近一次更新时间为 2015 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「timezone」 「timezones」 「timezonelist」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jackiedo/timezonelist 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jackiedo/timezonelist 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jackiedo/timezonelist 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Datetime helpers for timezone handling
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Timezone List Plugin
Official PHP SDK for the IPGeolocation.io IP Location API with single and bulk lookup support.
PHP Countries and Currencies
SierraTecnologia common support helpers, contracts, and traits required by various SierraTecnologia packages. Validator functionality, and basic controller included out-of-the-box.
统计信息
- 总下载量: 638.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 111
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-23
