doulanli6146 2016-11-19 15:42
浏览 33
已采纳

PHP + MySQL存储UTC时间戳与日期时间

I'm struggling to make a decision between timestamp and datetime. I did fint similar questions, but none gave me a definite answer.

The scenario is as follows:

  1. User creates an event in their calendar (using their timezone)
  2. Event is persised with date and time (in some "neutral" timezone)
  3. Another user can view this event (using their timezone)

I have to account for DST and all date & time related things. I believe dates should be stored in UTC timezone being neutral.

So my question is whether someone could provide me a workflow they utilized in this situation?

I was thinking that in PHP I would use DateTime obviously and:

  1. User creates an event => I take DateTime in his timezone -> convert it to timestamp (getTimestamp) and persist that way
  2. When some user wants to view that event => I fetch the timestamp from database -> create new DateTime object with his timezone -> set the timestamp (setTimestamp) and this way have that time displayed in his timezone

Is there any drawback of my workflow?

I will be using Symfony 2, so any Symfony 2 workflow would be best, but other than that I will take anything!

  • 写回答

1条回答 默认 最新

  • dozug64282 2016-11-19 16:31
    关注

    Your workflow is fine.

    Whenever you need to fulfil multi timezone requirement, it is better to store time in database as utc timestamp or unix epoch. unix epoch is well supported and i would suggest this. Even from the client side you can send unix epoch to the server without worrying about timezone. If you use utc timestamp you will need to convert between timezones. with unix epoch it is still conversion but it is very straight forward and well supported in most of the date object constructs.

    And when you need to retrieve and display time, you will construct the date object using unix epoch and the time zone for that user.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥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,如何解決?