dongshun5963 2011-12-09 10:19
浏览 168

在用户的时区显示当前时间,不要忘记夏令时

I know there are many similar questions, but I didn't find what I exactly need...

So I'm using PHP and I want to show the timestamps from database in the visitor's current timezone.

So far I have done the following:

  1. In PHP, I use $TIME everywhere, which is:

    $TIME = time() - date("Z");

    This should be an absolute reference to the time, with no dst, am I right?

  2. I get the user's timezone offset with javascript (only at the time of the first visit), and store it in minutes:

    var d = new Date(); var tz = -d.getTimezoneOffset();

    Of course, the user can set his/her timezone to another one.

  3. When displaying dates from database, I add the stored amount of time to the timestamp: $timestamp += $TIMEZONE_OFFSET * 60;

    And I format it with date()

This seems to be working well, but I wonder what if it comes to dst.

I thought of changing the user's timezone offset somehow automatically, when it comes to dst, but then all the dates would be changed (those too which are not in the dst period). And not every region uses dst, so I'm really confused how to figure this out :\

  • 写回答

2条回答 默认 最新

  • dongyuxin5353 2011-12-09 10:34
    关注

    First of all, time() and date() depends on INI setting date.timezone as well as on DST too. For absolute time you should use gmmktime() and gmdate() instead.

    Information about DST of timezone could be resolved, for example, with DateTimeZone::getTransitions().

    Or you can use some 3rd-party services instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题