php中的时区转换

时间:2010-03-24 06:08:51

标签: php datetime timezone timestamp

有人能建议一种简单的方法将日期和时间转换为php中的不同时区吗?

9 个答案:

答案 0 :(得分:101)

您可以使用datetime对象或其函数别名:

Example (abridged from PHP Manual)

date_default_timezone_set('Europe/London');

$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');

关于评论的修改

  

但我无法使用此方法,因为当用户从不同位置登录时,我需要在不同时区显示日期

这不是问题。当用户登录时,您确定他的时区并将其设置为DateTime对象,如图所示。我在我的一个项目中使用了类似的方法,它就像一个魅力。

  

在数据库中我需要在任何单个时区获取日期,然后才能正确处理

您可以将时间作为时间戳或日期时间存储在一个时区中。当您查询DateTime字段时,您可以将DateTime对象中的时间转换为此时区,或者 - 如果您的数据库支持它 - 使用所选时区进行查询。

答案 1 :(得分:17)

更简单的方法如下:

date_default_timezone_set('Europe/London'); // your user's timezone
$my_datetime='2013-10-23 15:47:10';
echo date('Y-m-d H:i:s',strtotime("$my_datetime UTC"));

如上所述in the PHP manual,strtotime()也会接受时区,您只需将其附加到日期时间。

我建议您将所有日期时间存储在UTC中,因为这样您就不会遇到夏令时问题。

答案 2 :(得分:11)

这对我有用,也很干净!

function convert_to_user_date($date, $format = 'n/j/Y g:i A', $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC')
{
    try {
        $dateTime = new DateTime ($date, new DateTimeZone($serverTimeZone));
        $dateTime->setTimezone(new DateTimeZone($userTimeZone));
        return $dateTime->format($format);
    } catch (Exception $e) {
        return '';
    }
}

function convert_to_server_date($date, $format = 'n/j/Y g:i A', $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC')
{
    try {
        $dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));
        $dateTime->setTimezone(new DateTimeZone($serverTimeZone));
        return $dateTime->format($format);
    } catch (Exception $e) {
        return '';
    }
}

//example usage
$serverDate = $userDate = '2014-09-04 22:37:22';
echo convert_to_user_date($serverDate);
echo convert_to_server_date($userDate);

答案 3 :(得分:7)

这些答案都不适用于我(我跳过了尝试过于庞大的代码)。我也认为仅为一次转换更改默认时区是很奇怪的。

这是我的解决方案:

function changeTimeZone($dateString, $timeZoneSource = null, $timeZoneTarget = null)
{
  if (empty($timeZoneSource)) {
    $timeZoneSource = date_default_timezone_get();
  }
  if (empty($timeZoneTarget)) {
    $timeZoneTarget = date_default_timezone_get();
  }

  $dt = new DateTime($dateString, new DateTimeZone($timeZoneSource));
  $dt->setTimezone(new DateTimeZone($timeZoneTarget));

  return $dt->format("Y-m-d H:i:s");
}

因此,要转换为服务器默认值,您只需传递一个时区:

changeTimeZone("2016-10-24 16:28", "Asia/Tokyo");

要将服务器默认值转换为用户,您可以将第二个参数保留为null或空白:

changeTimeZone("2016-10-24 16:28", "", "Asia/Tokyo");

要在与默认值无关的时区之间切换,您需要提供2个时区:

changeTimeZone("2016-10-24 16:28", "America/New_York", "Asia/Tokyo");

答案 4 :(得分:5)

DateTime::setTimezone -- date_timezone_set — Sets the time zone for the DateTime object

面向对象的风格

<?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";
?>

程序风格

<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";

date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>

以上示例将输出:

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

答案 5 :(得分:3)

UTC到本地:

<?php
$datetime = date("Y-m-d H:i:s");
$utc = new DateTime($datetime, new DateTimeZone('UTC'));
$utc->setTimezone(new DateTimeZone('America/Sao_Paulo'));
echo $utc->format('Y-m-d H:i:s');

?>

答案 6 :(得分:1)

//将日期从一个区域转换为另一个区域。 / *     $ zone_from = '亚/加尔各答';

$zone_to='America/Phoenix';

date_default_timezone_set($zone_from);

$convert_date="2016-02-26 10:35:00";

echo $finalDate=zone_conversion_date($convert_date, $zone_from, $zone_to);

*/
function zone_conversion_date($time, $cur_zone, $req_zone)
{   
    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d H:i:s");

    date_default_timezone_set($cur_zone);
    $local = date("Y-m-d H:i:s");

    date_default_timezone_set($req_zone);
    $required = date("Y-m-d H:i:s");

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

    $date = new DateTime($time);
    $date->modify("+$diff1 seconds");
    $date->modify("+$diff2 seconds");

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

答案 7 :(得分:0)

<?php
$time='6:02';
$dt = new DateTime($time, new DateTimeZone('America/New_York'));
//echo $dt->format('Y-m-d H:i:s') . PHP_EOL;
$dt->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $dt->format('H:i') . PHP_EOL; 
?>

答案 8 :(得分:0)

我总是很难记住setTimezone()方法是如何工作的。是否根据时区调整日期时间?还是要使用给定的日期时间,删除其时区并使用您通过的日期时间?总而言之,我希望有一种更直观的方式来处理时区。

我喜欢这个:

(new AdjustedAccordingToTimeZone(
    new DateTimeFromISO8601String('2018-04-25 15:08:01+03:00'),
    new Novosibirsk()
))
    ->value();

它以ISO8601格式输出日期时间,以新西伯利亚时区为准。

此方法使用蛋白酥皮库,请查看quick start以获得更多示例。