基本 PHP 时区转换
我查看了之前的问题,似乎没有什么符合我的要求。
我正在开发一个调度系统,现在客户想要添加时区。
所以基本上我需要能够在特定的日期和时间执行一项操作。
假设有人选择在以下日期/时间发出警报:
25-12-2010 13:01:00 + / - 他们的时区
我需要能够将其作为 unix_timestamp 存储在 mysql 中,我一直在查看 PHP 的DateTime 和 DateTimeZone 但这让我更加困惑。
我也查看了 strtotime,我只是不确定计算带有偏移量的日期/时间的最佳方法。
I have looked at previous questions and nothing seems to fit what I require.
I am working on a scheduling system and now the client wants to add timezones.
So basically I need to be able to proform an action at a specific date and time.
Lets say someone chooses to have an alert at the following date/time:
25-12-2010 13:01:00 + / - their timezone
I need to be able to store this in mysql as an unix_timestamp, I have been looking at PHP's DateTime and DateTimeZone
but this has confused me more.
I have looked at strtotime
to also, i am just unsure of the best way to work out the date/time with the offset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了确保我们意见一致,我只想重申一下,unix 时间戳只是自“Unix 纪元”(1970 年 1 月 1 日)以来的秒数。因此,简单的数学运算适用于 unix 时间戳。
有两种方法可以解决这个问题;从您的帖子来看,您对要使用哪个感到困惑。
第一种方法是最简单(也是最合乎逻辑)的方法,那就是存储它们的偏移量(如果您已经有的话)并将其乘以 3600(1 小时以秒为单位),然后添加 值到当前的unix时间戳以获得它们的最终运行时间。
另一种方法是使用
DateTime
和DateTimeZone
类。这两个类的工作原理,如此处所示,是您创建两个DateTimeZone
对象,一个包含您的时区,一个包含他们的时区;创建两个DateTime
对象,第一个参数是"now"
,第二个参数是对上面DateTimeZone
对象的引用(分别);然后在您的时区对象上调用 getOffset 方法,并将其时区对象作为第一个参数传递,最终获得以秒为单位的偏移量添加到当前的 unix 时间戳以获取其作业需要运行的时间。如果我自己这么说,对于如此简单的任务,第二种方法似乎要复杂得多,所以第一种解决方案可能更适合您的需求。但是,如果您希望有一个更完整的方法,那么使用
DateTime
和DateTimeZone
绝对是可能的。关于 strtotime 的快速说明:Strtotime 是 date() 命令的简单相反,并且不会比“工具”更有用来实现您正在寻找的东西。它本身不会为您转换或找到偏移量;它只是将格式化的日期和时间转换为 unix 时间戳。
Just to make sure we're on the same page, I just want to reiterate that a unix timestamp is merely the number of seconds since the "Unix Epoch" (January 1, 1970). Therefore simple math will work on unix timestamps.
There are two ways you could go about this; judging from your post you're confused as to which you'd like to use.
The first way would be the easiest (and most logical) way, and that is to store their offset (if you already have it, that is) and multiply that by 3600 (1 hour in seconds), and then add that value to the current unix timestamp to get their final time of running.
Another way to do it is to use the
DateTime
andDateTimeZone
classes. How these two classes work, as shown here, is that you create twoDateTimeZone
objects, one with your timezone and one with theirs; create twoDateTime
objects with the first parameters being"now"
and the second being the reference to theDateTimeZone
objects above (respectively); and then call thegetOffset
method on your timezone object passing their timezone object as the first parameter, ultimately getting you the offset in seconds that can be added to the current unix timestamp to get the time that their job needs to run.The second way seems much more complex for such an easy task if I do say so myself, so the first solution may suit your needs better. However, if you wish to have a more complete method, then using the
DateTime
andDateTimeZone
would definitely be a possibility.Quick note on strtotime: Strtotime is an easy opposite of the date() command, and won't be of any more use than a "tool" to achieve what you are looking for. It in and of itself will not convert or find offsets for you; it simply converts a formatted date and time into a unix timestamp.