0

Hi Everyone, I have a timezone variable that is stored in my database. In my code, I have it generate every timezone there is but what I need is to have it return the results of the selected="selected" when you go to pull the edit page up so you dont have to search 100's of different timezones to get yours again..

Here is the timezone code that I found:

<?php

function get_timezones()
{
    $o = array();

    $t_zones = timezone_identifiers_list();

    foreach($t_zones as $a)
    {
        $t = '';

        try
        {
            //this throws exception for 'US/Pacific-New'
            $zone = new DateTimeZone($a);

            $seconds = $zone->getOffset( new DateTime("now" , $zone) );
            $hours = sprintf( "%+02d" , intval($seconds/3600));
            $minutes = sprintf( "%02d" , ($seconds%3600)/60 );

            $t = $a ."  [ $hours:$minutes ]" ;

            $o[$a] = $t;
        }

        //exceptions must be catched, else a blank page
        catch(Exception $e)
        {
            //die("Exception : " . $e->getMessage() . '<br />');
            //what to do in catch ? , nothing just relax
        }
    }

    ksort($o);

    return $o;
}

$o = get_timezones();
?>

and to display the results:

<label>Timezone</label>
<select class="input" name="timezone">
<?php
    foreach($o as $tz => $label)

    {
        echo "<option value=".$tz.">$label</option>";
    }
?>
</select><br><br>

I need for the selected to go here somehow:

<label>Timezone</label>
<select class="input" name="timezone">
<?php
    foreach($o as $tz => $label)

    {
        echo "<option selected='selected' value=".$tz.">$label</option>";
    }
?>
</select><br><br>
2
Contributors
4
Replies
21
Views
4 Years
Discussion Span
Last Post by paulkd
0

I tried to use

<?php
    $dbValue= $row['timezn'];
    foreach($o as $tz => $label)
    $selected = ($tz == $dbValue ? "selected='selected'" : "");
    {
        echo "<option $selected value=".$tz.">$label</option>";
    }
?>

but all it submits back to me is zulu +0:00 when in the db it is America/Denver

0

Its a global variable for the site based of where I am.

Anyways, i got it fixed and made it way easier to use.

<label>Time Zone:</label><select name="timezone">
<?
$timezns = timezone_identifiers_list();
foreach ($timezns as $timezn) {
echo '<option';
if ( $timezn == $row['timezn'] ) echo ' selected';
echo ' value="' . $timezn . '">' . $timezn . '</option>' . "\n";
}
?>
</select>
0

You could write a function for your value testing:-

<?
function selected($expr)
{
    return $expr ? ' selected="selected"' : '';
}
?>

<label>Time Zone:</label><select name="timezone">
<?
$timezns = timezone_identifiers_list();
foreach ($timezns as $timezn) {
    echo '<option value="'.$timezn.'"'.selected($timezn == $row['timezn']).'>'.$timezn.'</option>';
}
?>
</select>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.