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

查看:29
本文介绍了在 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') . "
";

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

上面的例子会输出:

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

可在 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天全站免登陆