用PHP猜测用户的TimeZone [英] Guessing User's TimeZone in PHP

查看:82
本文介绍了用PHP猜测用户的TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从PHP5.3开始(我认为),已成为在php.ini文件中设置date.timezone或通过

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警告: date()注意:依靠系统的时区设置并不安全. 要求,您必须使用date.timezone设置或date_default_timezone_set()函数.如果您使用了这些方法中的任何一种,并且仍然收到此警告,则很可能您拼写了时区标识符.我们在 LINE 行的 FILE 中选择了"-4.0/DST"作为"-4.0/DST". /p>

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.

推荐答案

我可能会使用date_default_timezone_get()函数:

string date_default_timezone_get  ( void  )

此功能按优先顺序排列 通过以下方式返回默认时区:

In order of preference, this function returns the default timezone by:

  • 使用date_default_timezone_set()读取时区设置 功能(如果有)
  • 读取TZ环境变量(如果非空)(PHP之前的版本) 5.3.0)
  • 读取date.timezone ini选项的值(如果已设置)
  • 查询主机操作系统(如果受支持和允许) 操作系统)
  • 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/zh-CN/function.date-default-timezone-get.php

这篇关于用PHP猜测用户的TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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