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

问题描述:

基本上,我需要一个脚本,该脚本在提供时间和时区时可以返回另一个时区中的时间.

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

我的主要问题是:

  • 从哪里获取GMT的时间偏差-是否有公共数据库可用于此?
  • 如何也同时考虑夏令时(DST)的差异.
  • 如何将其全部包装到PHP类中?或者已经有这样的类了吗?

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

以上示例将输出:

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

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

就像Pekka所说的:DateTime类从5.2开始存在,您首先必须找出真正实现了哪些方法,而从5.3开始仅存在那些方法.

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.