Search code examples
phpdatetimetimezonedst

Check if timezone has DST


at the moment I am trying to detect if a timezone has DST at all and not if DST affects the timezone at the moment.

My code looks like this at the moment:

function timezoneDoesDST($tzId) {
    $tz = new DateTimeZone($tzId);
    return count($tz->getTransitions(time())) > 0;
}
$tz = new DateTimeZone('UTC');
$termin = new DateTime('2017-08-04 12:00:00',$tz);
$mytimezone=array();
foreach(timezone_abbreviations_list() as $abbr => $timezone){
    foreach($timezone as $val){
        if(isset($val['timezone_id'])){
            $found = 0;
            $tz2 = new DateTimeZone($val['timezone_id']);
            $termin->setTimezone($tz2);
            foreach($mytimezone as $mytz)
            {
                if($mytz['time'] == $termin->format('Y-m-d H:i:s'))
                {
                    $found = 1;
                    if(timezoneDoesDST($val['timezone_id']))
                    {
                        $found = 0;
                    }
                }
            }
            if($found == 0)
            {
                $mytimezone[] = array('time' => $termin->format('Y-m-d H:i:s'),'timezone_id' => $val['timezone_id']);
            }
        }
    }
}
foreach($mytimezone as $mytz)
{
    echo $mytz['time'].' - '.$mytz['timezone_id'].'<br />';
}

The thing is, I won't to display all different timezones with a different time and timezones which has a DST at all.

But at the moment it displays me all timezones and if I remove this:

if(timezoneDoesDST($val['timezone_id']))
{
    $found = 0;
}

It just displays me timezones with different time

Thanks


Solution

  • Ok I have updated my code a bit to reduce the amount of selected timezones if anybody needs this:

        function timezoneDoesDST($tzId) {
            $tz = new DateTimeZone($tzId);
            return count($tz->getTransitions(time())) > 0;
        }
        $tz = new DateTimeZone('UTC');
        $termin = new DateTime('2017-08-04 12:00:00',$tz);
        $mytimezone=array();
        $y = 0;
        foreach(timezone_abbreviations_list() as $abbr => $timezone){
    
            foreach($timezone as $val){
                if(isset($val['timezone_id'])){
                    $y++;
                    $found = 0;
                    $found2 = 0;
                    $tz2 = new DateTimeZone($val['timezone_id']);
                    $termin->setTimezone($tz2);
                    foreach($mytimezone as $mytz)
                    {
                        if($mytz['time'] == $termin->format('Y-m-d H:i:s'))
                        {
                            $found = 1;
                        }
                        if(timezoneDoesDST($val['timezone_id']))
                        {
                            if($mytz['time'] == $termin->format('Y-m-d H:i:s') and $mytz['timezone_id'] != $val['timezone_id'] and $mytz['DST'] == 0)
                            {
                                $found2 = 1;
                                $found = 0;
                            }
                        }
                    }
                    if($found == 0)
                    {
                        $mytimezone[] = array('time' => $termin->format('Y-m-d H:i:s'),'timezone_id' => $val['timezone_id'],'DST' => $found2);
                    }
                }
            }
        }
        $mylist = count($mytimezone);
        echo $mylist.' - '.$y.'<br /><br />';
        foreach($mytimezone as $mytz)
        {
            echo $mytz['time'].' - '.$mytz['DST'].' - '.$mytz['timezone_id'].'<br />';
        }
    

    This reduces it from about 1500 to 68 different timezones with and without DST.

    Cheers and thanks to all.