Question: I have a full list of timezones in a select menu like so:

```
<option value="Pacific/Kosrae"> Pacific/Kosrae( +11:00 GMT ) </option>
  <option value="Pacific/Kwajalein"> Pacific/Kwajalein( +12:00 GMT ) </option>
  <option value="Pacific/Majuro"> Pacific/Majuro( +12:00 GMT ) </option>
  <option value="Pacific/Marquesas"> Pacific/Marquesas( -09:30 GMT ) </option>
  <option value="Pacific/Midway"> Pacific/Midway( -11:00 GMT ) </option>

```

the list goes on forever.

I want to change each of the options into this format:

```
if($_SESSION['timezone'] == 'Africa/Abidjan') {
echo '<option selected="selected" value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>'; 
} else {
echo '<option value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>';
}

```

How can I use php to avoid having to copy paste and edit each of the options manually??

Answer: 
Store the data in some data structure, and use a loop. For example, using a map from timezone name to offset:

```
$timezones = array(
    'Pacific/Kosrae' => '+11:00',
    'Pacific/Kwajalein' => '+12:00',
    ...
);

foreach($timezones as $name => $offset) {
    echo "<option value=\"$name\"" . ($name == $_SESSION['timezone'] ? " selected" : "") . ">$name( $offset GMT ) </option>\n";
}

```
Ok, you don't have to it with [DOM](http://de2.php.net/manual/en/book.dom.php), but since there is already plenty of other answers showing different approaches, here is how to do it with DOM:

```
function timezoneHelper($selected = NULL)
{   
    $dom = new DOMDocument;
    $dom->formatOutput = TRUE;
    $dom->loadXML('<select/>');
    $dom->documentElement->setAttribute('name', 'timezone-selector');
    $timezones = DateTimeZone::listIdentifiers();
    if(!is_numeric($selected)) {
        $selected = array_search($selected, $timezones);
    }
    foreach($timezones as $id => $timezone) {
        $option = $dom->createElement('option', $timezone);
        $option->setAttribute('value', $id);
        if($id == $selected) {
            $option->setAttribute('selected', 'selected');
        }
        $dom->documentElement->appendChild($option);
        unset($option);
    }
    return $dom->saveXML($dom->documentElement);
}

```

The above will create a list of timezone identifiers (w\out the GMT diff though. See [`DateTimeZone::listAbbreviations`](http://de2.php.net/manual/en/datetimezone.listabbreviations.php) if you need them) as `<option>` elements in a `<select>` element. Unlike your code in the question, the value used for the `value` attribute is the numeric offset in the array returned by [`DateTimeZone::listIdentifiers();`](http://de2.php.net/manual/en/datetimezone.listidentifiers.php) instead of the timezone identifier itself. You can invoke the helper with the timezone identifier or the numeric ID though, e.g.

```
echo timezoneHelper('551');
// or
echo timezoneHelper('Zulu');

```

would both markup

```
 <option value="551" selected="selected">Zulu</option>

```

with the `selected` attribute in the list returned.