在PHP中将时间和日期从一个时区转换为另一个时区 [英] Convert time and date from one time zone to another in PHP

查看:196
本文介绍了在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.

这篇关于在PHP中将时间和日期从一个时区转换为另一个时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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