以 UTC 格式存储日期和时间,并在 PHP/MySQL 中将其转换为本地时间
在我对如何处理日期和时间进行全面重构之前,我想问一下这是否可行。
为了提出我的问题,这是我的问题...... 我将用户采取的“操作”存储在历史表中。这些操作都有时间戳。用户可以来自世界各地,因此具有不同的时区。我想显示历史记录中的项目并显示本地时区的时间戳。
另外,我知道我可以使用日期时间字段并简单地存储 UTC 时间格式,但我选择使用时间戳,这样我就可以将默认值设置为当前时间。
这对我的问题有用吗?
我的服务器时区将是洛杉矶
将时间戳存储为 MySQL 中的时间戳。我的理解是时间戳始终存储为 UTC。记录将默认存储为creation_date,或者我将显式设置UTC_TIMESTAMP()。实际上,数据库中的所有记录都将采用 UTC。
将当用户登录时,我将获取他们的时区(我将其作为用户设置存储在数据库中)。 对于这个例子,假设这个人在纽约。
这就是我的问题所在。这些都会起作用吗?
- a) 抓取历史数据。所有时间戳都会自动从 UTC 转换为洛杉矶时间。对于每个历史记录项,将洛杉矶时间戳转换为纽约时间戳并回显。
或者
- b) 将 php 时区“date_default_timezone_set”设置为纽约。抓取历史数据。所有时间戳仍然是洛杉矶时间,服务器仍然是洛杉矶时间。对于每个历史记录项,只需回显时间戳,因为“date_default_timezone_set”会自动将洛杉矶时间戳转换为纽约时间戳。
选项 4b 有效吗?有没有更好的方法从洛杉矶时间转换为纽约时间?
一般来说有更好的方法吗?
I wanted to ask if this was going to work, before I went on a full fledged refactor of how i handle dates and times.
To frame my question, here's my problem...
I store "actions" taken by the user in a history table. These actions have a timestamp on them. Users can be from all over the world, and hence have different timezones. I want to display the items in the history and show the timestamps in one's local timezone.
Also, I know I can use datetime fields and simply store UTC time formats, but I'm opting to use timestamp so i can set a default to current time.
Will this work for my problem?
My server time zone will be Los Angeles
Store timestamps as a timestamp in MySQL. My understanding is that timestamp is always stored as UTC. Records will be stored by default as creation_date OR i will explicitly set UTC_TIMESTAMP(). Effictively, all records in the db will be UTC.
When a user logs in I will grab their timezone (I store this in the db as a user setting).
For this example, lets say this person is in New York.
Here's where my question is. Will either of these work?
- a) Grab the history data. All timestamps will automatically be converted from UTC to Los Angeles time. Foreach history item, convert the Los Angeles timestamp to New York timestamp and echo.
OR
- b) Set the php timezone "date_default_timezone_set" to New York. Grab the history data. All timestamps are STILL in Los Angeles time by server is still Los Angeles time. Foreach history item, just echo the timestamp because the "date_default_timezone_set" will automatically convert the Los Angeles timestamp to New York.
Will option 4b work? Is there a better way to convert from the Los Angeles time to the New York time?
Is there a better way to do this in general?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是对的,Unix 时间戳始终采用 UTC。如果您以 UTC 格式将时间戳存储在数据库中,则要以本地时间输出它,您只需将 PHP 中的时区更改为用户本地的时区即可。您不需要对时间戳进行任何转换。
您可以即时更改时区并继续以日期格式输出时间戳,它们将根据 PHP 中设置的时区进行“转换”。这很简单,不需要做任何特殊处理,并且在哪里设置时区并不重要,只要在输出日期之前进行即可,但是您可以在设置时区之前读取它们。
You are correct, Unix timestamps are always in UTC. If you store the timestamps in your database in UTC, all you need to do to output it in local time is change the timezone in PHP to the timezone local to the user. You don't need to do any conversion on the timestamps.
You can change your timezone on the fly and continue to output timestamps in a date format and they will be "converted" based on the set timezone in PHP. It is very easy, you don't need to do any special handling, and it doesn't matter where you set the timezone, as long as you do it before you output dates, but you can read them prior to setting the timezone.
不幸的是,“date_default_timezone_set”并没有按照我想象的方式工作。根据前面的示例,如果我正在使用的日期已经是 unix 时间戳格式,那么它就会起作用。然而,它们的格式为“Ymd H:i:s”,唯一会改变的是 GMT 偏移量,这显然不起作用。
经过几个小时的烦躁之后,以下是我处理时间转换的方法。
我想我也可以执行以下操作:
我认为,上面的内容将强制日期函数在给定的 $user_timezone 中处理其逻辑。事后看来,我必须翻译每个 $datetime -> unix_timestamp ->; $datetime_converted,这听起来并不比我选择做的好多少。
Unfortunately, "date_default_timezone_set" was not working the way I thought it would. As per the previous example, it would work if the dates I was working with were already in unix timestamp format. However, they were formatted in "Y-m-d H:i:s" and the only thing that would change was the GMT offset, which obviously doesnt work.
After a few hours of fidgeting, here's how I was able to handle time conversions.
I suppose that I could have done the following as well:
the above, i think, would force the date function to handle it's logic in the given $user_timezone. in hindsight, i would have to translate every $datetime -> unix_timestamp -> $datetime_converted, which doesnt sound to be much better than what i chose to do.
始终将时间戳作为 UTC 存储在数据库中。这是一个独立于时区和夏令时的时间点。要将其显示回用户的时区,只需执行以下操作:
Always store your timestamps in the database as UTC. This is a timezone and daylight savings independent point in time time. To display this back to the user in their timezone, simply do:
默认情况下,Unix 时间戳始终采用 UTC。如果我们以 UTC 格式将时间戳存储在数据库中,则只需使用以下 PHP 脚本:
By default, Unix timestamps are always in UTC. If we store the timestamps in our database in UTC, we just need to use the following PHP script: