dqmnueuu459279 2013-12-04 12:15
浏览 44
已采纳

PHP时区更换器

In PHP you can change the timezone with a simple line of code

date_default_timezone_set('Europe/London');

My problem I encounter is making a timezone changer that is user based. I want to have the user use a drop box with timezone options and using MySQL keep it saved on the user's data variables. My issue is related to how the time zones are handled by PHP, specifically with the names that are given to each variable. It is confusing enough having time zones with names like the following.

  • Europe/London
  • Asia/Shanghai
  • America/Dawson

That isn't the only problem, there are so many the numbers of different time zone names reach the hundreds. I am not sure of those hundreds which one to use because I obviously cannot use them all as that would be a bit redundant and inefficient. I would use time zones like GMT+5 or GMT-1 but the PHP manual says I shouldn't use them, and from what I read they do not support DST. I am wondering what I should do in order to get a good list of time zones that would be good for my purposes. Or if someone is kind enough they could forward me to a list that I could use.

I am looking for any list that can cover most time zones.

TL;DR I need help in figuring out what time zones to use for a time zone drop box.

  • 写回答

2条回答 默认 最新

  • douzhaxian1267 2013-12-04 13:01
    关注

    If you store a timezone string (for example 'Europe/London') for each user, you can convert incoming times to UTC, then reconvert them upon display. That's pretty easy once you figure it out:

        $previoustz = date_default_timezone_get();
        @date_default_timezone_set( $users_chosen_timezone );
        /* do your time processing here */
        @date_default_timezone_set( $previoustz );
    

    You do have to be disciplined about always doing this, or your system will display some wrong-time bugs.

    As for the question of what time zones to offer, I strongly suggest you offer the whole list provided by PHP. You only have to gather a timezone setting from a user in a user-profile screen, so you're not constantly hassling users with a long dropdown list.

    You need them all for for a few reasons. One is the general bizarreness of timezones in the USA, where some local jurisdictions do strange things with daylight savings time. Check out America/Indiana. This from the state in which the legislature once decreed the value of π to be 3. Do you want to figure all this out? I didn't think so.

    A second reason is this: some jurisdictions switch back and forth from daylight savings on hard-to-automate dates. For example, the state of Israel switches based on the Hebrew calendar. You want your users simply to choose their timezone.

    Another is this: people using global systems like WordPress have come to expect the standard set of choices, which are based on a venerable Unix / Linux system called timezoneinfo. If you just go with the standard, you can let the timezoneinfo people take care of what choices you should offer. This is an international electropolitical issue, and timezoneinfo deals with it for you far better than you could hope to.

    Take a look at http://www.php.net/manual/en/datetimezone.listidentifiers.php for a way to obtain the list.

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

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题