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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效