<i id='i9VuZ'><tr id='i9VuZ'><dt id='i9VuZ'><q id='i9VuZ'><span id='i9VuZ'><b id='i9VuZ'><form id='i9VuZ'><ins id='i9VuZ'></ins><ul id='i9VuZ'></ul><sub id='i9VuZ'></sub></form><legend id='i9VuZ'></legend><bdo id='i9VuZ'><pre id='i9VuZ'><center id='i9VuZ'></center></pre></bdo></b><th id='i9VuZ'></th></span></q></dt></tr></i><div id='i9VuZ'><tfoot id='i9VuZ'></tfoot><dl id='i9VuZ'><fieldset id='i9VuZ'></fieldset></dl></div>
    <bdo id='i9VuZ'></bdo><ul id='i9VuZ'></ul>

<small id='i9VuZ'></small><noframes id='i9VuZ'>

      <legend id='i9VuZ'><style id='i9VuZ'><dir id='i9VuZ'><q id='i9VuZ'></q></dir></style></legend>
        <tfoot id='i9VuZ'></tfoot>

        使用 DateTime/DateTimeZone 在 PHP 中调整时区

        Adjusting time zone in PHP with DateTime / DateTimeZone(使用 DateTime/DateTimeZone 在 PHP 中调整时区)
      1. <small id='uYSbR'></small><noframes id='uYSbR'>

        <i id='uYSbR'><tr id='uYSbR'><dt id='uYSbR'><q id='uYSbR'><span id='uYSbR'><b id='uYSbR'><form id='uYSbR'><ins id='uYSbR'></ins><ul id='uYSbR'></ul><sub id='uYSbR'></sub></form><legend id='uYSbR'></legend><bdo id='uYSbR'><pre id='uYSbR'><center id='uYSbR'></center></pre></bdo></b><th id='uYSbR'></th></span></q></dt></tr></i><div id='uYSbR'><tfoot id='uYSbR'></tfoot><dl id='uYSbR'><fieldset id='uYSbR'></fieldset></dl></div>
          • <bdo id='uYSbR'></bdo><ul id='uYSbR'></ul>
          • <legend id='uYSbR'><style id='uYSbR'><dir id='uYSbR'><q id='uYSbR'></q></dir></style></legend>
            <tfoot id='uYSbR'></tfoot>
                  <tbody id='uYSbR'></tbody>

                  本文介绍了使用 DateTime/DateTimeZone 在 PHP 中调整时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有很多关于在 PHP 中进行时区调整的信息,但由于所有的噪音,我还没有找到具体我想要做什么的答案.

                  There's a lot of info on doing time zone adjustments in PHP, but I haven't found an answer for specifically what I want to do due to all the noise.

                  给定一个时区的时间,我想将其转换为另一个时区的时间.

                  Given a time in one timezone, I want to convert it to the time in another timezone.

                  这个本质上就是我想做的,但我需要能够仅使用内置的 PHP 库而不是 PEAR Date 来做到这一点.

                  This is essentially what I want to do, but I need to be able to do it using only the built-in PHP libs, not PEAR Date.

                  这是我一直在做的,但它似乎总是给我相对于 GMT 的偏移量:

                  This is what I've been doing, but it seems to always give me the offset relative to GMT:

                  $los_angeles_time_zone = new DateTimeZone('America/Los_Angeles');
                  $hawaii_time_zone = new DateTimeZone('Pacific/Honolulu');
                  
                  $date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone);
                  printf("LA Time: %s<br/>", $date_time_los_angeles->format(DATE_ATOM));
                  
                  $time_offset = $hawaii_time_zone->getOffset($date_time_los_angeles);
                  printf("Offset: %s<br/>", $time_offset);
                  

                  这是输出:

                  洛杉矶时间:2009-09-18T05:00:00-07:00
                  偏移量:-36000

                  LA Time: 2009-09-18T05:00:00-07:00
                  Offset: -36000

                  我期待 3 小时(10800 秒).但是 '-7:00' 告诉我它保留了相对于 GMT 的所有内容,这也许可以解释为什么它给了我绝对"偏移量.

                  I was expecting 3 hours (10800 seconds). but the '-7:00' thing tells me it's keeping everything relative to GMT, which maybe explains why it's giving me the "absolute" offset.

                  如何在没有所有这些 GMT 的情况下获得两个时区之间的偏移量?

                  How do I just get the offset between the two timezones without all of this GMT hoohah?

                  谢谢.

                  更新:

                  我突然想到我可以这样做并得到我想要的:

                  I occured to me that I could do this and get what I want:

                      $date_time_los_angeles = new DateTime('2009-09-18 05:00:00', $los_angeles_time_zone);
                  printf("LA Time: %s<br/>", $date_time_los_angeles->format(DATE_ATOM));
                  
                  $date_time_hawaii = new DateTime('2009-09-18 05:00:00', $hawaii_time_zone);
                  printf("Hawaii Time: %s<br/>", $date_time_hawaii->format(DATE_ATOM));
                  
                  
                  $time_offset = $los_angeles_time_zone->getOffset($date_time_los_angeles) - $hawaii_time_zone->getOffset($date_time_los_angeles);
                  printf("Offset: %s<br/>", $time_offset);
                  

                  但这对我来说感觉很尴尬.有人知道更清洁的方法吗?

                  But it feels awkward to me. Anyone know a cleaner way to do it?

                  推荐答案

                  这里有几个使用 DateTime 类的函数.第一个将返回两个时区之间的秒差.第二个返回时间从一个时区到另一个时区的转换".

                  Here are a couple of functions using the DateTime classes. The first one will return the difference in seconds between two timezones. The second returns a "translation" of the time from one timezone to another.

                  function timezone_diff($tz_from, $tz_to, $time_str = 'now')
                  {
                      $dt = new DateTime($time_str, new DateTimeZone($tz_from));
                      $offset_from = $dt->getOffset();
                      $timestamp = $dt->getTimestamp();
                      $offset_to = $dt->setTimezone(new DateTimezone($tz_to))->setTimestamp($timestamp)->getOffset();
                      return $offset_to - $offset_from;
                  }
                  
                  function time_translate($tz_from, $tz_to, $time_str = 'now', $format = 'Y-m-d H:i:s')
                  {
                      $dt = new DateTime($time_str, new DateTimezone($tz_from));
                      $timestamp = $dt->getTimestamp();
                      return $dt->setTimezone(new DateTimezone($tz_to))->setTimestamp($timestamp)->format($format);
                  }
                  

                  演示:

                  $los_angeles_time = '2009-09-18 05:00:00';
                  $los_angeles_tz   = 'America/Los_Angeles';
                  $hawaii_tz        = 'Pacific/Honolulu';
                  
                  $los_angeles_hawaii_diff = timezone_diff($los_angeles_tz, $hawaii_tz, $los_angeles_time);
                  echo $los_angeles_hawaii_diff . '<br />';
                  
                  $hawaii_time = time_translate($los_angeles_tz, $hawaii_tz, $los_angeles_time);
                  echo $hawaii_time . '<br />';
                  

                  这篇关于使用 DateTime/DateTimeZone 在 PHP 中调整时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='oScis'></bdo><ul id='oScis'></ul>
                  • <tfoot id='oScis'></tfoot>

                        <small id='oScis'></small><noframes id='oScis'>

                          <tbody id='oScis'></tbody>

                        1. <i id='oScis'><tr id='oScis'><dt id='oScis'><q id='oScis'><span id='oScis'><b id='oScis'><form id='oScis'><ins id='oScis'></ins><ul id='oScis'></ul><sub id='oScis'></sub></form><legend id='oScis'></legend><bdo id='oScis'><pre id='oScis'><center id='oScis'></center></pre></bdo></b><th id='oScis'></th></span></q></dt></tr></i><div id='oScis'><tfoot id='oScis'></tfoot><dl id='oScis'><fieldset id='oScis'></fieldset></dl></div>

                          <legend id='oScis'><style id='oScis'><dir id='oScis'><q id='oScis'></q></dir></style></legend>