PHP 5.4无法自行确定时区 [英] PHP 5.4 Can't Determine Time Zones on its own

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

问题描述

我只是在寻找一点澄清。 PHP 5.4消除了TZ环境变量以及 date_default_timezone_get 曾经做过的猜测。所以现在在我看来,根本没办法得到服务器的时区。



所以我的问题是:是否可以在PHP 5.4中获取服务器时区?我知道我可以在php.ini手动设置它,但是当有一台电脑完全有能力知道什么时间的时候,这似乎是一种愚蠢的事情。我希望答案是否,所以也许有人可以说明为什么编程语言将无法确定它的时区,如果是这样的话。

解决方案

没有可靠的方式来猜测每个系统的时区。不同的系统使用不同的约定,有时甚至不同的时区定义(例如Unix和Windows)有时具有冲突的时区说明符(相同的3字母区域缩写可能意味着不同的地方)。所以如果你想要在系统之间移植代码,唯一可靠的方法是询问用户。参见例如此线程:
http://marc.info/ ?t = 132356551500002& r = 1& w = 2 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。



如果您有办法找出您信任的时区(例如TZ变量),那么您可以随时执行 date_default_timezone_set($ _ SERVER [' TZ']); 。但是总的来说这不是一个可靠的方法,所以一定是你决定信任它,PHP不能为你做。


I'm just looking for a little clarification on this. PHP 5.4 eliminated the TZ environmental variable and the "guessing" that date_default_timezone_get used to do. So now it appears to me there's no way to get the server's timezone at all.

So my question is this: is it possible to get the server timezone in PHP 5.4? I know I can manually set it in php.ini, but that seems kind of silly when there's a computer that's perfectly capable of knowing what time it is. I expect the answer is "no", so perhaps somebody can shed some light on why a programming language would be unable to determine its time zone if that's the case.

解决方案

There is no reliable way to guess timezone on every system. Different systems use different conventions and sometimes even different timezone definitions (such as Unix and Windows), sometimes have conflicting timezone specifiers (the same 3-letter zone abbreviation can mean different things in different places). So if you want to have code that is portable between systems, the only way to reliably do it is to ask the user. See for example this thread: http://marc.info/?t=132356551500002&r=1&w=2 on some of the issues with it (look for emails from Derick Rethans - he is the PHP datetime extension maintainer).

If you have a way to find out timezone that you trust - such as TZ variable - then you could always do date_default_timezone_set($_SERVER['TZ']);. However in general this is not a reliable method, so it must be your decision to trust it, PHP can not make it for you.

这篇关于PHP 5.4无法自行确定时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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