dongxun2089 2013-09-08 20:59
浏览 210
已采纳

如何将PHP时区字符串(例如Europe / Berlin)转换为codeigniter时区(例如UP1)

In a short if i have to ask this question is just the vice verse of this link question .

I am using a javscript library which automcatically detects the timezone and returns the time zone name in PHP format i.e 'Europe/Berlin' format.

But I make use of Codeigniters timezone_menu which gives a drop down box of time timezones and I am wondering how can i convert this php time zone (i.e 'Europe/Berlin' ) to codeigniters timezone format i.e UP1.?

  • 写回答

2条回答 默认 最新

  • dongpei2835 2013-09-08 21:58
    关注

    It appears from the CodeIgniter documentation that they are treating time zones as fixed offsets. Their convention is fairly straight forward:

    • UTC to represent exactly UTC.

      or

    • U to represent UTC

    • M or P for Minus or Plus

    • A number to describe the offset

      • A single digit for whole hour offsets
      • Two digits when there are half-hour offsets, but these are shifted funky (you would think +05:30 would be represented by UP55, but its actually UP45

    So why did they do this? Who knows. It's not a standard thing, it's a CodeIgnitor special format. Normally an offset is just represented like +05:30 or +0530.

    Using an offset to represent a whole time zone is out of sync with reality. If it was that easy, then we wouldn't need the IANA time zones like Europe/Berlin in the first place. You can see in the time zone list here that Europe/Berlin alternates between +01:00 and +02:00. Code Ignitor might say that it is UP1, but then that wouldn't ever take into account the daylight time offset. (Daylight saving time is different all over the world, so you can't just augment this with a checkbox and expect to be reliable.)

    So, if you must have CodeIgnitor's strange form of time zone representation, then take the base offset of the IANA zone and apply their funky formula (as dev-null-dweller showed in his answer). Just don't expect it to be accurate.

    And if you're in zone with a :45 offset, then you're out of luck. That would be Pacific/Chatham or Asia/Kathmandu.

    You can read more in the timezone tag wiki under "Time Zone != Offset"

    Just to add something actually constructive to to this answer, I recommend not using CodeIgnitor's time zones. Just stick with the IANA zones as provided for you by PHP. They are even kept up to date via timezondb in the PECL.

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

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟