duankun9280 2010-10-11 10:05
浏览 94
已采纳

在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.

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?
  • 写回答

4条回答 默认 最新

  • douyan2002 2010-10-11 10:08
    关注
    <?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') . "
    ";
    ?>
    

    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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容