如何通过PHP从UTC轻松转换日期? [英] How can I easily convert dates from UTC via PHP?

查看:393
本文介绍了如何通过PHP从UTC轻松转换日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UTC的datetime字段中将日期存储在MySQL数据库中.我使用的是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轻松转换日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆