从 PHP5.3 开始(我认为),date.timezone
需要在 php.ini
文件中设置,或者设置为运行时通过 date_default_timezone_set()
函数.当您不设置此值时,您会得到一个错误,类似于以下内容:
PHP 警告:date(): 依赖系统的时区设置是不安全的.您必须使用 date.timezone 设置或 date_default_timezone_set() 函数.如果您使用了这些方法中的任何一种,但仍然收到此警告,您很可能拼错了时区标识符.我们在 LINE 行的 FILE 中选择了America/New_York"作为-4.0/DST".
我想知道它使用什么机制来猜测时区,因为它似乎非常准确,我想自己知道,以便我可以以更友好的方式向用户呈现此错误,或者让他们接受这是正确的时区.
我可能会使用 date_default_timezone_get()
函数:
string date_default_timezone_get ( void )
按优先顺序,此功能通过以下方式返回默认时区:
http://www.php.net/manual/en/function.date-default-timezone-get.php
As of PHP5.3 (I think) it became a requirement for the date.timezone
to be set in the php.ini
file, or to be set at runtime via the date_default_timezone_set()
function. When you don't set this value you get, an error, something along the lines of this:
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for '-4.0/DST' instead in FILE on line LINE.
I was wondering what mechanism it uses to guess the timezone, as it seems to be pretty accurate, and I wanted to know for myself so I can present this error in a more friendly way to the user, or allow them to accept this as the correct timezone.
I'd probably use the date_default_timezone_get()
function:
string date_default_timezone_get ( void )
In order of preference, this function returns the default timezone by:
- Reading the timezone set using the date_default_timezone_set() function (if any)
- Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
- Reading the value of the date.timezone ini option (if set)
- Querying the host operating system (if supported and allowed by the OS)
http://www.php.net/manual/en/function.date-default-timezone-get.php
这篇关于在 PHP 中猜测用户的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!