在 PHP 中将时间和日期从一个时区转换为另一个时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3905193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:26:50  来源:igfitidea点击:

Convert time and date from one time zone to another in PHP

phptimezone

提问by titel

Basically what I need is an script that, when provided with a time and a timezone can return the time in another time zone.

基本上我需要的是一个脚本,当提供时间和时区时可以返回另一个时区的时间。

My main issues are:

我的主要问题是:

  • Where to get the time offset from GMT from - is there a public database available for this?
  • How to also take into consideration the daylight saving time (DST) differences as well.
  • How to nicely wrap it all up inside an PHP class - or is there such a class already available?
  • 从哪里获得格林威治标准时间的时间偏移量 - 是否有可用的公共数据库?
  • 如何同时考虑夏令时 (DST) 差异。
  • 如何将其全部封装在 PHP 类中 - 或者是否已经有这样的类?

回答by ITroubs

<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

The above examples will output:

上面的例子将输出:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

found on DateTime Manual on php.net

在 php.net上的DateTime 手册上找到

EDIT: Like Pekka said: The DateTime class exists from 5.2 on and there you first have to find out which of the methods are realy implemented and which one only exist from 5.3 on.

编辑:就像 Pekka 说的:DateTime 类从 5.2 开始存在,在那里你首先必须找出哪些方法是真正实现的,哪些只存在于 5.3 之后。

回答by Shubham Mathur

try this, it might help :)

试试这个,它可能会有所帮助:)

function converToTz($time="",$toTz='',$fromTz='')
    {   
        // timezone by php friendly values
        $date = new DateTime($time, new DateTimeZone($fromTz));
        $date->setTimezone(new DateTimeZone($toTz));
        $time= $date->format('Y-m-d H:i:s');
        return $time;
    }

A bit description:The function takes 3 inputs, time to convert, timezone to convert to, current timezone and returns the output in the specified format.

一点说明:该函数接受 3 个输入,转换时间、转换到的时区、当前时区并以指定格式返回输出。

回答by srp

I know its late. For anyone who would want simple function to convert utc to any local time zone

我知道已经晚了。对于任何想要简单函数将 utc 转换为任何本地时区的人

function UTCTimeToLocalTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
    $tz = date_default_timezone_get();

$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
    DateTimeZone('UTC'));
$local_datetime = $utc_datetime;

$local_datetime->setTimeZone(new DateTimeZone($tz));
return $local_datetime->format($ToDateFormat);
}

 echo UTCTimeToLocalTime('2015-07-01 13:30:00','America/Denver');

回答by user2622929

Here i use this function for converting datetime into another timezone. For best result if you convert your datetime into utc timezone and then convert into required timezone then it is better result for it.

在这里,我使用此函数将日期时间转换为另一个时区。为了获得最佳结果,如果您将日期时间转换为 UTC 时区,然后转换为所需的时区,那么它的结果会更好。

function ConvertTimezoneToAnotherTimezone($time, $currentTimezone, $timezoneRequired) {
    $dayLightFlag = false;
    $dayLgtSecCurrent = $dayLgtSecReq = 0;
    $system_timezone = date_default_timezone_get();
    $local_timezone = $currentTimezone;
    date_default_timezone_set($local_timezone);
    $local = date("Y-m-d H:i:s");
    /* Uncomment if daylight is required */
    //        $daylight_flag = date("I", strtotime($time));
    //        if ($daylight_flag == 1) {
    //            $dayLightFlag = true;
    //            $dayLgtSecCurrent = -3600;
    //        }
    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d H:i:s ");

    $require_timezone = $timezoneRequired;
    date_default_timezone_set($require_timezone);
    $required = date("Y-m-d H:i:s ");
    /* Uncomment if daylight is required */
    //        $daylight_flag = date("I", strtotime($time));
    //        if ($daylight_flag == 1) {
    //            $dayLightFlag = true;
    //            $dayLgtSecReq = +3600;
    //        }

    date_default_timezone_set($system_timezone);

    $diff1 = (strtotime($gmt) - strtotime($local));
    $diff2 = (strtotime($required) - strtotime($gmt));

    $date = new DateTime($time);

    $date->modify("+$diff1 seconds");
    $date->modify("+$diff2 seconds");

    if ($dayLightFlag) {
        $final_diff = $dayLgtSecCurrent + $dayLgtSecReq;
        $date->modify("$final_diff seconds");
    }

    $timestamp = $date->format("Y-m-d H:i:s ");

    return $timestamp;
}

Thank You.

谢谢你。