Laravel Timezone List
, (*1)
Overview
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, (*2)
Timezone-List branch |
Laravel version |
4.x |
4.x |
5.x |
5.x and later |
This documentation is use for branch 5.x, (*3)
Documentation
Installation
You can install this package through Composer with the following steps:, (*4)
Step 1 - Require Package
At the root of your application directory, run the following command (in any terminal client):, (*5)
$ 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:, (*6)
Step 2 - Register Service Provider
Open config/app.php
, and add a new line to the providers section:, (*7)
...
Jackiedo\Timezonelist\TimezonelistServiceProvider::class,
Step 3 - Register Facade Alias
Add the following line to the aliases section in file config/app.php
:, (*8)
'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., (*9)
Example:, (*10)
<?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., (*11)
Example: (use in the resources/views/demo.blade.php
file), (*12)
<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., (*13)
Example:, (*14)
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:, (*15)
/**
* 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, (*16)
Example:, (*17)
echo Timezonelist::toSelectBox('timezone');
This will output the following HTML code:, (*18)
<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:, (*19)
- 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:, (*20)
// 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
, (*21)
, (*22)
Render a timezone array
Syntax:, (*23)
/**
* Create a timezone array.
*
* @param bool $htmlencode Use HTML entities for items
*
* @return mixed
*/
public function toArray($htmlencode = true);
Example:, (*24)
$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):, (*25)
- 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:, (*26)
Get only some specified groups
Syntax:, (*27)
/**
* Set the filter of the groups want to get.
*
* @param array $groups
*
* @return $this
*/
public function onlyGroups($groups = []);
Example:, (*28)
...
$return = Timezonelist::onlyGroups(['Asia', 'America'])->toSelectBox('timezone');
Exclude some specified groups
Syntax:, (*29)
/**
* Set the filter of the groups do not want to get.
*
* @param array $groups
*
* @return $this
*/
public function excludeGroups($groups = []);
Example:, (*30)
...
$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:, (*31)
Decide whether to split group or not
Syntax:, (*32)
/**
* Decide whether to split group or not.
*
* @param bool $status
*
* @return $this
*/
public function splitGroup($status = true);
Example:, (*33)
$return = Timezonelist::splitGroup(false)->excludeGroups(['General'])->toSelectBox('timezone');
Decide whether to show the timezone offset or not
Syntax:, (*34)
/**
* Decide whether to show the offset or not.
*
* @param bool $status
*
* @return $this
*/
public function showOffset($status = true);
Example:, (*35)
$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:, (*36)
Syntax:, (*37)
/**
* Return new static to reset all config.
*
* @return $this
*/
public function reset();
Example:, (*38)
// 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., (*39)
License
MIT © Jackie Do, (*40)