PHP code example of jackiedo / timezonelist

1. Go to this page and download the library: Download jackiedo/timezonelist library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jackiedo / timezonelist example snippets


...
Jackiedo\Timezonelist\TimezonelistServiceProvider::class,

'Timezonelist' => Jackiedo\Timezonelist\Facades\Timezonelist::class,



namespace Your\Namespace;

use Jackiedo\Timezonelist\Facades\Timezonelist;

class YourClass
{
    public function yourMethod()
    {
        $return = Timezonelist::doSomething();
    }
}


<div class="form-group">
    {!! Timezonelist::doSomething() !!}
</div>

namespace Your\Namespace;

use Jackiedo\Timezonelist\Timezonelist;

class YourClass
{
    public function yourMethod()
    {
        $timezoneList = new Timezonelist;

        $return = $timezoneList->doSomething();
    }
}


/**
 * 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);

echo Timezonelist::toSelectBox('timezone');

// 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"');

/**
 * Create a timezone array.
 *
 * @param bool $htmlencode Use HTML entities for items
 *
 * @return mixed
 */
public function toArray($htmlencode = true);

$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",
//         ...
//     ],
//     ...
// ]

/**
 * Set the filter of the groups want to get.
 *
 * @param array $groups
 *
 * @return $this
 */
public function onlyGroups($groups = []);

...
$return = Timezonelist::onlyGroups(['Asia', 'America'])->toSelectBox('timezone');

/**
 * Set the filter of the groups do not want to get.
 *
 * @param array $groups
 *
 * @return $this
 */
public function excludeGroups($groups = []);

...
$return = Timezonelist::excludeGroups(['General'])->toArray();

/**
 * Decide whether to split group or not.
 *
 * @param bool $status
 *
 * @return $this
 */
public function splitGroup($status = true);

$return = Timezonelist::splitGroup(false)->excludeGroups(['General'])->toSelectBox('timezone');

/**
 * Decide whether to show the offset or not.
 *
 * @param bool $status
 *
 * @return $this
 */
public function showOffset($status = true);

$return = Timezonelist::showOffset(false)->excludeGroups(['General'])->toSelectBox('timezone');

/**
 * Return new static to reset all config.
 *
 * @return $this
 */
public function reset();

// 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
html
<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>