Guessing User#39;s TimeZone in PHP(在 PHP 中猜测用户的时区)
问题描述
从 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 )
按优先顺序,此功能通过以下方式返回默认时区:
- 使用 date_default_timezone_set() 读取时区设置功能(如果有)
- 读取 TZ 环境变量(如果非空)(PHP 之前5.3.0)
- 读取 date.timezone ini 选项的值(如果已设置)
- 查询主机操作系统(如果支持并允许操作系统)
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 中猜测用户的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中猜测用户的时区
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01