Eastern Standard Time (EST)?

Hello,

I have script with the following option:


..
<option value="-5">(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima
..

I recently discovered that my script was not working correctly.

But then I saw that according to this website

the time zone for Eastern Time is -4!!

I thought, what the hell? Did I get the timezone wrong the first time? Or does this have to do with daylight saving?

It’s daylight savings. EST changes between GMT-4 and GMT-5.

I typically use this to get a drop-down selection of timezones. Most of this originally came from the wordpress code base.

The function you’ll want to use to get the option elements is get_timezone_form_field_options($selectedValue)

/**     * returns the list of timezones in an array
     * @param bool $returnValues[optional] return an array of only the timezone values
     * @return array timezones in format: $location[group/continent][php value]=display value; if $returnValues=true, timezone values returned as array
     */
    function get_timezone_list($returnValues=false){
        $locations=array(); //locations[continent/group][php value] = display value
    
        $continents = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific');
    
        //add continent/city timezones
        foreach (timezone_identifiers_list() as $zone){
            $zone = explode('/', $zone); // 0 => Continent, 1 => City
    
            //only use "friendly" continent names
            if(in_array($zone[0], $continents)){
                if(isset($zone[1]) && $zone[1]!=""){
                    if(isset($zone[2]) && $zone[1]!=""){
                        $locations[$zone[0]][$zone[0].'/'.$zone[1].'/'.$zone[2]] = str_replace('_', SPACE, $zone[1]).SPACE.'-'.SPACE.str_replace('_', SPACE, $zone[2]); // Creates array(DateTimeZone => 'Friendly name')
                    }
                    else{
                        $locations[$zone[0]][$zone[0].'/'.$zone[1]] = str_replace('_', SPACE, $zone[1]); // Creates array(DateTimeZone => 'Friendly name')
                    }
                }
            }
        }
    
        //add UTC timezone
        $locations['UTC']['UTC']='UTC';
    
        //add manual offset timezones
        $offset_range = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14);
    
        foreach($offset_range as $offset){
            $offset_name = ($offset>=0) ? '+'.$offset : (string)$offset;
            $offset_value = $offset_name;
            $offset_name = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset_name);
            $offset_name = 'UTC'.$offset_name;
            $offset_value = 'UTC'.$offset_value;
            $locations['Manual Offset'][$offset_value]=$offset_name;
        }
    
        if($returnValues===true){
            $temp=array();
            foreach($locations as $key=>$val){
                $temp[]=$key;
            }
            $locations=$temp;
        }
    
    
        return $locations;
    }
    
    
    
    function get_timezone_form_field_options($selectedvalue=""){
        $output="";
    
        $timezones = get_timezone_list();
    
        $input_field=array();
    
        foreach($timezones as $group => $group_val){
            foreach($group_val as $value => $name){
                $input_field[$group][] = array("name"=>$name, "value"=>$value);
            }
        }
    
        foreach($input_field as $groupname => $group){
            if($groupname===""){
                $output.=getFormFieldOptions($group, $selectedvalue);
            }
            else{
                $output.='<optgroup label="'.$groupname.'">';
                $output.=getFormFieldOptions($group, $selectedvalue);
                $output.='</optgroup>';
            }
        }
    
        return $output;
    }
    

	//gets an individual option for an HTML select field and marks a match as selected when appropriate
	function get_selectoption_value($name, $value, $selectedvalue=""){
		if(!is_string($name)){ $name=$value; }
		$selected="";
		if($value==$selectedvalue){ $selected=' selected="selected"'; }
		return '<option value="'.$value.'"'.$selected.'>'.$name.'</option>';
	}

    
    function getFormFieldOptions($group, $selectedvalue=""){
        $optionTags="";
        foreach($group as $option){
            $value=$option['value'];
            $name=(isset($option['name'])) ? $option['name'] : $value;
            $optionTags.=get_selectoption_value($name, $value, $selectedvalue);
        }
        return $optionTags;
    }

Maybe if I explain my problem then you guys can help me out better.

I have a website that provides free polls. I’m currently rewriting the scripts so that users can add a publish date & time for their poll. So the poll only accepts vote after the publish date.

But the users also need to enter a time zone. Because if the users enters Sept 15, 2012, 21:00 then I need to know the time zone, so I can compare it to my server’s date and time. Basically I do the following comparison:


// if system date < publish date
if (mktime(date("H"),date("i"),0,date("m"),date("d"),date("Y")) - (60 * 60 * $server_timezone) < mktime($publish_hour,$publish_minute,0,$publish_month,$publish_day,$publish_year) - (60 * 60 * $publish_timezone))
{/* poll not open for votes yet */}
else
{/* users can start to vote */}

I was just testing with the Eastern Time when I noticed that my -5 wasn’t working. Anyone kow a better way to solve this problem?

I have not looked into your code, but if you change your system to use DateTime instead (use the OOP version! ;), it will be a much easier task for you.

Then locally in the script base it on GMT for example, then you convert back/forth depending on the visitors timezone.

Example:


$date = new DateTime('2012-09-12 20:29:00' new DateTimeZone('CET')); //My time and timezone right now

$date->setTimezone(new DateTimeZone('GMT')); //Changing the object to GMT timezone

$date->modify('+30 minutes'); //Adding 30 minutes to the object

echo $date->format('m/d/Y H:i:s'); //Displaying the date/time

When programming today, there is no reason why you should not use the DateTime functionality instead of mktime/date/time etc.

http://no2.php.net/manual/en/datetime.construct.php

Edit:
Thought I should add what the echo should display: In this case if I am not mistaken it will show: 09/12/2012 18:59:00

Uhmmm…thanks…but I do NOT know OOP, just procedural programming. So I have no idea what your code means.

I got to learn me some OOP someday!

DateTime offers two ways that you can use it.

One way is procedural code, similar to mkdate etc. and the other is object oriented, which is the version I used in my example.

I would strongly recommend you to read the PHP manual for this functionality, and also use the version I used in my example, as it makes it much easier to read and understand the code later on. I.e. which date/time you are modifying etc.

Ok, thanks!

I agree with @TheRedDevil ; approach of using the OOP of DateTime, as then you only need to know the Time Zone abbreviation to be able to change the poll date accordingly. Will save you a LOT of work.