PHP code example of realodix / timezone

1. Go to this page and download the library: Download realodix/timezone 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/ */

    

realodix / timezone example snippets


/**
 * @param string $name The name attribute of the select tag
 * @param string|null $selected The value of the option to be pre-selected
 * @param array|null $attrs Additional HTML attributes
 */
public function toSelectBox(string $name, ?string $selected = null, ?array $attrs = null): string

use Realodix\Timezone\Timezone;

$tz = new Timezone;
$attributes = ['class' => 'form-control', 'id' => 'timezone-select'];

// Generate a select box with the name "timezone", pre-selecting "America/New_York",
// and using the specified attributes.
$tz->toSelectBox('timezone', 'America/New_York', $attributes);

use Realodix\Timezone\Timezone;

$tz = new Timezone;
$tz->toArray();

[
    'UTC' => '(UTC+00:00) UTC',
    'America' => [
        'Nassau' => '(UTC-05:00) Nassau',
        'New_York' => '(UTC-05:00) New York',
        'Nome' => '(UTC-09:00) Nome'
        // ...
    ],
    // ...
]

use Realodix\Timezone\Timezone;

$tz = new Timezone;
// Only t:
// [
//     'America' => [
//         'Nassau' => '(UTC-05:00) Nassau',
//         'New_York' => '(UTC-05:00) New York',
//         // ...
//     ],
//     'Europe' => [
//         'London' => '(UTC+00:00) London',
//         // ...
//     ],
// ]

// Exclude timezones from the Arctic and Africa groups.
$tz->excludeGroups(['Arctic', 'Africa'])->toArray();

// Output (will not 
html
<select name="timezone" class="form-control" id="timezone-select">
    <optgroup label="General">
        <option value="UTC">(UTC+00:00) UTC</option>
    </optgroup>
    ...
    <optgroup label="America">
        ...
        <option value="America/Nassau">(UTC-05:00) Nassau</option>
        <option value="America/New_York" selected>(UTC-05:00) New York</option>
        <option value="America/Nome">(UTC-09:00) Nome</option>
        ...
    </optgroup>
    ...
    <optgroup label="Asia">
        <option value="Asia/Aden">(UTC+03:00) Aden</option>
        <option value="Asia/Almaty">(UTC+05:00) Almaty</option>
        ...
    </optgroup>
    ...
</select>