1. <small id='K9iDX'></small><noframes id='K9iDX'>

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

        • <bdo id='K9iDX'></bdo><ul id='K9iDX'></ul>
      1. 如何通过 PHP 轻松地从 UTC 转换日期?

        How can I easily convert dates from UTC via PHP?(如何通过 PHP 轻松地从 UTC 转换日期?)
          <bdo id='iRFqZ'></bdo><ul id='iRFqZ'></ul>

          • <legend id='iRFqZ'><style id='iRFqZ'><dir id='iRFqZ'><q id='iRFqZ'></q></dir></style></legend>
            <tfoot id='iRFqZ'></tfoot>
                <tbody id='iRFqZ'></tbody>

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

                <i id='iRFqZ'><tr id='iRFqZ'><dt id='iRFqZ'><q id='iRFqZ'><span id='iRFqZ'><b id='iRFqZ'><form id='iRFqZ'><ins id='iRFqZ'></ins><ul id='iRFqZ'></ul><sub id='iRFqZ'></sub></form><legend id='iRFqZ'></legend><bdo id='iRFqZ'><pre id='iRFqZ'><center id='iRFqZ'></center></pre></bdo></b><th id='iRFqZ'></th></span></q></dt></tr></i><div id='iRFqZ'><tfoot id='iRFqZ'></tfoot><dl id='iRFqZ'><fieldset id='iRFqZ'></fieldset></dl></div>
                1. 本文介绍了如何通过 PHP 轻松地从 UTC 转换日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将日期存储在 MySQL 数据库中的 UTC 日期时间字段中.我正在使用 PHP,并且我调用了 date_timezone_set('UTC') 以便所有对 date() 的调用(不带时间戳)都以 UTC 格式返回日期.

                  I am storing dates in a MySQL database in datetime fields in UTC. I'm using PHP, and I've called date_timezone_set('UTC') so that all calls to date() (without timestamp) return the date in UTC.

                  然后我拥有它,以便给定的网站可以选择其时区.现在我希望日期显示在站点的时区中.因此,如果我将日期存储为2009-04-01 15:36:13",它应该在 PDT 时区(-7 小时)中为用户显示为2009-04-01 08:36:13".

                  I then have it so a given web site can select its timezone. Now I want dates to display in the site's timezone. So, if I have a date stored as '2009-04-01 15:36:13', it should display for a user in the PDT timezone (-7 hours) as '2009-04-01 08:36:13'.

                  通过 PHP 执行此操作的最简单(最少代码)的方法是什么?到目前为止,我想到的只是

                  What is the easiest (least code) method for doing this via PHP? So far all I've thought of is

                  date('Y-m-d H:i:s', strtotime($Site->getUTCOffset() . ' hours', strtotime(date($utcDate))));
                  

                  有没有更短的方法?

                  推荐答案

                  这是我们对服务器所做的.我们将所有内容设置为使用 UTC,并通过即时转换 UTC 来显示在用户的时区.本文底部的代码是如何使其工作的示例;您应该确认它适用于您的设置(即夏令时等)的所有情况.

                  Here's what we did with our servers. We set everything to use UTC, and we display in the user's time zone by converting from UTC on the fly. The code at the bottom of this post is an example of how to get this to work; you should confirm that it works in all cases with your setup (i.e. daylight savings, etc).

                  1. 编辑 /etc/sysconfig/clock 并将 ZONE 设置为 UTC
                  2. ln -sf/usr/share/zoneinfo/UTC/etc/localtime
                  1. Edit /etc/sysconfig/clock and set ZONE to UTC
                  2. ln -sf /usr/share/zoneinfo/UTC /etc/localtime

                  配置 MySQL

                  1. 必要时将时区导入 MySQL:

                  1. Import timezones into MySQL if necessary:

                  mysql_tzinfo_to_sql/usr/share/zoneinfo |mysql -u root -p mysql

                  编辑 my.cnf 并在 [mysqld] 部分添加以下内容:

                  Edit my.cnf and add the following within the [mysqld] section:

                  default-time-zone = 'UTC'

                  PHP 代码

                  <?php
                  /*
                  Example usage:
                    $unixtime = TimeUtil::dateTimeToTimestamp('2009-04-01 15:36:13');
                    echo TimeUtil::UTCToPST("M d, Y - H:i:s", $unixtime);
                  */
                  
                  // You should move this to your regular init method
                  date_default_timezone_set('UTC'); // make this match the server timezone
                  
                  class TimeUtil {
                      public static function timestampToDateTime($timestamp) {
                          return gmdate('Y-m-d H:i:s', $timestamp);
                      }
                  
                      public static function dateTimeToTimestamp($dateTime) {
                          // dateTimeToTimestamp expects MySQL format
                          // If it gets a fully numeric value, we'll assume it's a timestamp
                          // You can comment out this if block if you don't want this behavior
                          if(is_numeric($dateTime)) {
                              // You should probably log an error here
                              return $dateTime;
                          }
                          $date = new DateTime($dateTime); 
                          $ret = $date->format('U');
                          return ($ret < 0 ? 0 : $ret);
                      }
                  
                      public static function UTCToPST($format, $time) {
                          $dst = intval(date("I", $time));
                          $tzOffset = intval(date('Z', time()));
                          return date($format, $time + $tzOffset - 28800 + $dst * 3600);
                      }
                  
                  }
                  

                  这篇关于如何通过 PHP 轻松地从 UTC 转换日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

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

                      <tbody id='LQx3l'></tbody>
                        • <legend id='LQx3l'><style id='LQx3l'><dir id='LQx3l'><q id='LQx3l'></q></dir></style></legend>

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