【问题标题】:Timezone and summertime CodeIgniter时区和夏令时 CodeIgniter
【发布时间】:2015-08-11 05:12:12
【问题描述】:

我寻找更好的解决方案来查看我网站的所有用户的当地时间。

我遇到了夏令时问题,如果你有更好的方法帮助我,我实际上有这个功能:

function timezone_by_offset($offset) {


 $abbrarray = timezone_abbreviations_list();
    $offset = $offset * 60 * 60;

    foreach ($abbrarray as $abbr) {
        foreach ($abbr as $city) {
            if ($city['offset'] == $offset && $city['dst'] == TRUE) { 
                return $city['timezone_id'];                                
            }
        }
    }
    return 'UTC'; // any default value you wish
}
echo timezone_by_offset(1) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(1)) == TRUE ? 'Valid'. date('Y-m-d H:i:s') : 'Not Valid'). '<br/>';

解决方案:

<?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();
?>

<html>
<body>
<select name="time_zone">
<?php
    foreach($o as $tz => $label)
    {
        echo "<option value="$tz">$label</option>";
    }
?>
</select>
</body>
</html>

最好的问候。

【问题讨论】:

    标签: php codeigniter timezone dst timezone-offset


    【解决方案1】:

    更新 3
    第 1 部分 - 找到正确的时区
    如果您正在寻找 timezone_name 的偏移量和夏时制,您将 timezone_name_from_abbr() 的第三个参数设置为 true,否则设置为 false。如果您将其设置为 1 并且偏移量 + 夏令时没有给出有效的 timezone_name 它将返回 false。在这种情况下,您可以将其转回 false。
    示例

    $tz = timezone_name_from_abbr(null, $offset * 3600, true);
    if($tz === false) $tz = timezone_name_from_abbr(null, $offset * 3600, false);
    

    第 2 部分 - 根据 DAYLIGHT SAVINGS 获取本地日期/时间
    您无需在此处执行任何操作,一旦设置了时区,PHP 日期函数会根据您的时区和当前日期自动设置夏令时。所以

    function print_according_to_offset($offset)
    {
    
        $tz = timezone_name_from_abbr(null, $offset, true); //If a ofset with daylight savings is found
        if($tz === false) $tz = timezone_name_from_abbr(null, $offset, false); //otherwise
        if($tz === false) // If no timezone is still found
        {
            echo 'Invalid Offset <br />';
            return;
        }
        $otherTZ  = new DateTimeZone($tz); 
        $datetime = new DateTime; // current time = server time
        $datetime->setTimezone($otherTZ);
        echo $tz . '<br />';
        echo $datetime->format('Y-m-d H:i:s') .'<br /><br />';
    }
    
    print_according_to_offset(19800);
    

    更新 2

    <?php
    $timezone = '+0:00';
    $timezone = preg_replace('/[^0-9]/', '', $timezone) * 36;
    $timezone_name = timezone_name_from_abbr(null, $timezone, true);
    date_default_timezone_set($timezone_name);
    echo date('D d M Y H:i:s');
    ?>
    

    More information
    更新
    CI功能及改进代码

    function print_according_to_offset($offset)
    {
        $user_timezone =  timezone_name_from_abbr("",$offset,0);
        $otherTZ  = new DateTimeZone($user_timezone);
        $datetime = new DateTime; // current time = server time
        $datetime->setTimezone($otherTZ);
        echo $datetime->format('Y-m-d H:i:s') .'<br />';
    }
    
    print_according_to_offset(14400); //EXAMPLE
    

    不要指望我们为您做所有事情,如果您只需要获取时区,请编辑函数并返回 $otherTZ。
    旧帖
    我想这就是你需要的。

    $offset = 3600 ; //your offset
    $my_timezone =  timezone_name_from_abbr("",$offset,0);
    
    date_default_timezone_set($my_timezone);
    echo date('Y-m-d H:i:s');
    

    More info

    【讨论】:

    • 时区不固定,我网站上的用户是国际用户,我使用 codeigniter 框架,我有一个获取时区的功能(UTC+1、UTC+2、UTC+3 等)但是我不知道如何正确地做到这一点
    • 在php中,CI是基于php的。它肯定会与 CI 一起运行,您所需要的只是设置偏移量。但我已经更新了解决方案,因此您可以使用 CI 轻松实施
    • 我以为你找到了正确的偏移量。我已经更新了我的帖子,现在应该可以使用了。
    • 好的,但是这种方式的问题是夏季时间和冬季时间,我知道在西欧时间是冬季的 UTC +1 和夏季的 UTC +2,我不知道如何这样做,您的功能无法正常工作,因为世界上所有国家/地区都没有夏令时,我无法执行 UTC -1、UTC -2 等
    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2012-10-31
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多