如何通过 PHP 轻松地从 UTC 转换日期?

2022-01-16 00:00:00 timezone php

我将日期存储在 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);
    }

}

相关文章