PHP - date() 与 date.timezone / date_default_timezone_set()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10308315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - date() vs. date.timezone / date_default_timezone_set()
提问by Nick
I've just got a new computer, and I've been setting up PHP/MySQL/databases etc... I think I'm just about there, except it's thrown this curveball. My login script was working fine, but now it's spitting the following warning (which messes up the JSON).
我刚买了一台新电脑,我一直在设置 PHP/MySQL/数据库等......我想我就在那里,只是它抛出了这个曲线球。我的登录脚本工作正常,但现在它发出以下警告(这会弄乱 JSON)。
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are requiredto 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 'Antarctica/Macquarie' for 'EST/10.0/no DST' instead in .../php/login.phpon line 47
警告:date() [function.date]:依赖系统的时区设置是不安全的。您 需要使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了这些方法中的任何一种并且仍然收到此警告,则很可能是您拼错了时区标识符。我们在第47行的 .../php/login.php 中为“EST/10.0/no DST”选择了“Antarctica/Macquarie”
My code obviously uses date()and is working in the live version and on the old machine. I get two warnings for the following two lines of code:
我的代码显然使用date()并在实时版本和旧机器上工作。我收到以下两行代码的两个警告:
$date = date("ymd");
$this_year = date("y");
My research (see here) suggests that the behaviour of these functions depends on php.ini .
我的研究(参见此处)表明这些函数的行为取决于 php.ini 。
So should I change php.ini on the new machine, or am I using some kind of deprecated method, and should I ditch date()altogether?
那么我应该在新机器上更改 php.ini ,还是我使用某种已弃用的方法,我应该date()完全放弃吗?
Thanks.
谢谢。
回答by John Conde
You don't need to change the php.ini file if you use date_default_timezone_set(). Just set it to the timezone you will be working in.
如果使用 .php,则无需更改 php.ini 文件date_default_timezone_set()。只需将其设置为您将使用的时区即可。
Something like this should go in a config file or on the page where you're working with dates (if it is only one page):
像这样的内容应该放在配置文件或您正在处理日期的页面上(如果它只有一页):
date_default_timezone_set('America/Los_Angeles');
回答by jeroen
It's not an exception, it's a warning that is probably popping up now because your error reporting settings on the new machine are different from the old one.
这不是一个例外,它是一个警告,现在可能会弹出,因为您在新机器上的错误报告设置与旧机器不同。
I would suggest to follow the suggestion in the warning, and use date_default_timezone_set()to set a time-zone in the scripts where you need it.
我建议遵循警告中的建议,并使用date_default_timezone_set()在脚本中需要的地方设置时区。
回答by tosin
First, you are although it may know, this solves the problem.
首先,您虽然可能知道,但这解决了问题。
date_default_timezone_set('Your/Timezone');
If you want to configure in php.ini, modify below
如果要在php.ini中配置,修改如下
[Date]
; ...
; ...
date.timezone = Your/Timezone
回答by Haluk
You do not have to change the php.ini.
您不必更改 php.ini。
However, if you have a timezone which you will use for most of your your php files adding the following line to your php.ini should do the trick.
但是,如果您有一个时区,您将在大多数 php 文件中使用该时区,那么在 php.ini 中添加以下行应该可以解决问题。
date.timezone = "Your/Timezone"
In my case I added the following line to php.ini. Personally, I prefer to keep all our servers at UTC timezone.
就我而言,我在 php.ini 中添加了以下行。就我个人而言,我更喜欢将所有服务器保持在 UTC 时区。
date.timezone = "UTC"

