Storing specific Date/Time values for users in different time zones

前端 未结 1 513
南笙
南笙 2021-01-24 04:20

I\'m working on a PHP(CodeIgniter)/MySQL application that allows users select when their blog post will be published. How I have designed it to work so far is as below:

相关标签:
1条回答
  • 2021-01-24 04:41

    Since you are talking about scheduling of future time, storing only UTC isn't necessarily the best approach.

    Keep in mind that time zone rules can (and do) change. If you apply an update to your time zone data (in PHP, it's done with PECL's timezonedb package), then any data you already converted to UTC might be invalid.

    The better solution is to store multiple values:

    • The original local date and time

    • The original time zone (ex. "America/New_York")

    • The UTC date and time, converted from the local values

    When you want to see if it's time to run the task, you'd compare the current UTC date and time against the stored UTC value.

    When you apply time zone updates, you throw out the previous converted UTC values for all future entries, and recalculate new ones based on the updated data.

    If you don't do this, then you're losing the intent that the user originally provided. And if you don't keep your time zone data updated, then your system won't be aware of various changes happening around the world, such as the changes made in Egypt earlier this year, or the changes coming in October for Russia.

    Last point - If implemented properly, the time zone of your server should not matter. Compare UTC to UTC.

    0 讨论(0)
提交回复
热议问题