基本PHP时区转换 [英] Basic PHP timezones conversion

查看:180
本文介绍了基本PHP时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过以前的问题,没有什么似乎符合我的要求。



我正在调度系统,现在客户端想添加时区。 / p>

所以基本上我需要能够在特定的日期和时间进行动作。



让某人说选择在以下日期/时间提醒:



25-12-2010 13:01:00 + / - 他们的时区



我需要能够将它存储在mysql中作为unix_timestamp,我一直在看PHP的DateTime和 DateTimeZone ,但这更混淆了我。



我也看过 strtotime ,我只是不确定最好的方法来制定日期/时间与偏移。

解决方案

只是为了确保我们在同一页面,我只想重申一个unix时间戳只是Unix Epoch(1970年1月1日)以来的秒数。因此,简单的数学将适用于unix时间戳。



有两种方法可以解决这个问题:从你的帖子判断你对你想要使用的东西感到困惑。



第一种方式将是最简单(最符合逻辑)的方法,那就是存储它们的偏移量(如果你已经拥有了),并将其乘以3600(以1秒为单位),然后将 值添加到当前的unix时间戳,以获得最后的运行时间



另一种方法是使用 DateTime DateTimeZone 类。这两个类如何工作,如 here 所示,您创建了两个 DateTimeZone 对象,一个与你的时区,一个与他们的;创建两个 DateTime 对象,其中第一个参数是now,第二个参数是 DateTimeZone 上面的对象(分别);然后调用您的时区对象的 getOffset 方法,将他们的时区对象作为第一个参数,最终让您以秒为单位,可以添加到当前的unix时间戳以获取他们的作业需要运行的时间。



第二种方式似乎很多如果我自己这么说,这样一个容易的任务更复杂,所以第一个解决方案可以更好地满足你的需求。但是,如果您希望使用更完整的方法,则使用 DateTime DateTimeZone 肯定是一种可能性。



在strtotime上的快速注意事项:Strtotime与date()命令的对比很方便,并不会比工具更容易使用,正在找。它本身不会转换或找到你的补偿;它只需将格式化的日期和时间转换为unix时间戳。


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.

解决方案

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 and DateTimeZone classes. How these two classes work, as shown here, is that you create two DateTimeZone objects, one with your timezone and one with theirs; create two DateTime objects with the first parameters being "now" and the second being the reference to the DateTimeZone objects above (respectively); and then call the getOffset 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 and DateTimeZone 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.

这篇关于基本PHP时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆