在 PHP 中处理时区和日期
我正在美国的服务器上运行一项托管服务,该服务读取使用本地日期创建的 XML 提要(目前仅限英国),但我想确保该服务适用于所有时区。 我的流程查看提要中帖子的日期,并将其与当前的日期/时间(在美国的服务器上)进行比较。 我提出的解决方案将系统本地化为提要的发起者,然后创建一个时间戳来与“现在”进行比较:
protected function datemath($thedate){
$currenttimezone = date_default_timezone_get();
date_default_timezone_set($this->feedtimezone);
$thedate = mktime substr($thedate,11,2),substr($thedate,14,2),
substr($thedate,17,2),substr($thedate,3,2),substr($thedate,0,2),
substr($thedate,6,4));
date_default_timezone_set($currenttimezone);
return $thedate;
}
我的问题是......这是处理此问题的合理方法还是有更好的方法,我真正应该知道的更标准化的方式?
I am running a service hosted on a server in the US which reads an XML feed that has been created with a local date - currently just the UK, but I want to ensure the service works with all timezones.
My process looks at the date of a post in a feed and compares it with the date/time right now(on the server in the US).
The solution I came up with localises the system to the originator of the feed and then creates a timestamp with which to compare 'now' with:
protected function datemath($thedate){
$currenttimezone = date_default_timezone_get();
date_default_timezone_set($this->feedtimezone);
$thedate = mktime substr($thedate,11,2),substr($thedate,14,2),
substr($thedate,17,2),substr($thedate,3,2),substr($thedate,0,2),
substr($thedate,6,4));
date_default_timezone_set($currenttimezone);
return $thedate;
}
My question is this... Is this a reasonable way of handling this issue or is there a better, more standardized way that I really should know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我编写的用于进行时区转换的函数。应该是不言自明的:
Here's a function I wrote to do timezone conversions. Should be pretty self-explanatory:
经过更多检查其他人的代码后,我发现该函数
比使用 mktime 更简洁,并且还允许不同的时间格式。
After a bit more checking of other peoples code I see the function
is a little bit more succinct than using mktime and also allows for different time formats.