【问题标题】:Convert Unix Timestamp to time zone?将 Unix 时间戳转换为时区?
【发布时间】:2013-05-20 23:14:48
【问题描述】:

我有一个设置为 +5 的 unix 时间戳,但我想将其转换为 -5,即 EST 标准时间。我只想在那个时区生成时间戳,但我从另一个来源获取它,它把它放在 +5 处。

当前未修改的时间戳被转换为日期

<? echo gmdate("F j, Y, g:i a", 1369490592) ?>

【问题讨论】:

    标签: php unix datetime timezone timestamp


    【解决方案1】:

    使用DateTimeDateTimeZone

    $dt = new DateTime('@1369490592');
    $dt->setTimeZone(new DateTimeZone('America/Chicago'));
    echo $dt->format('F j, Y, g:i a');
    

    【讨论】:

    • 如果我错了,请纠正我@John 这肯定行不通吗?此方法将使用在创建 DateTime 对象的实例时未指定的系统(PHP 配置)默认时区。喜欢here。因此,当转换为 America/Chicago 时,它会根据系统时区进行更改,假设它与 +5 不同,例如他试图更改约 10 个区域的 UNIX 时间戳本身?
    • @Stefan 这确实是错误的。 Unix 时间戳始终为 UTC。一些有趣的代码示例: ini_set('date.timezone', 'America/Los_Angeles'); $now = time(); $utc = new DateTime("@{$now}", new DateTimeZone('America/New_York')); echo "{$utc->format('c')}\n"; // 2014-08-27T15:24:09+00:00 $utc = new DateTime("@{$now}"); echo "{$utc->format('c')}\n"; // 2014-08-27T15:24:09+00:00 ini_set('date.timezone', 'UTC'); $utc = new DateTime("@{$now}", new DateTimeZone('UTC')); echo "{$utc->format('c')}\n"; // 2014-08-27T15:24:09+00:00
    • 我要提出一些建议:如果您的 UNIX 时间戳是一个变量,则必须使用双引号使其像这样工作:`new DateTime("@$timestamp") ;如果使用单引号或不使用引号,则会出现错误。
    【解决方案2】:

    因为John Conde's answer 的编辑队列已满,我将添加更详细的答案。

    来自DateTime::__construct(string $time, DateTimeZone $timezone)

    $timezone 参数和当前时区被忽略 $time 参数要么是 UNIX 时间戳(例如 @946684800)...

    这就是为什么在从 unix 时间戳创建 DateTime 对象时应始终指定时区(甚至是默认时区)的主要原因。查看受John Conde's answer启发的解释代码:

    $dt = new DateTime('@1369490592');
    
    // use your default timezone to work correctly with unix timestamps
    // and in line with other parts of your application
    date_default_timezone_set ('America/Chicago'); // somewhere on bootstrapping time
    …
    $dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
    
    // set timezone to convert time to the other timezone
    $dt->setTimeZone(new DateTimeZone('America/Chicago'));
    
    echo $dt->format('F j, Y, g:i a');
    

    【讨论】:

      【解决方案3】:

      一个更简单的方法是:

      使用gmdate() 时,将您的时区以秒为单位添加到 gmdate 中的 unix_stamp。

      假设我的时区是 GMT+5:30。所以 5 hr 30 min 秒内将是 19800

      所以,我会这样做:

      gmdate("F j, Y, g:i a", 1369490592+19800)

      【讨论】:

        【解决方案4】:

        这是一个将 unix/gmt/utc 时间戳转换为所需时区的函数,您可能会感兴趣。

        function unix_to_local($timestamp, $timezone){
            // Create datetime object with desired timezone
            $local_timezone = new DateTimeZone($timezone);
            $date_time = new DateTime('now', $local_timezone);
            $offset = $date_time->format('P'); // + 05:00
        
            // Convert offset to number of hours
            $offset = explode(':', $offset);
            if($offset[1] == 00){ $offset2 = ''; }
            if($offset[1] == 30){ $offset2 = .5; }
            if($offset[1] == 45){ $offset2 = .75; }
            $hours = $offset[0].$offset2 + 0;
        
            // Convert hours to seconds
            $seconds = $hours * 3600;
        
            // Add/Subtract number of seconds from given unix/gmt/utc timestamp
            $result = floor( $timestamp + $seconds );
        
            return $result;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-25
          • 2015-10-07
          • 2021-12-18
          • 2013-04-07
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 2020-10-19
          相关资源
          最近更新 更多