如何修复 PHP 中 date() 的警告"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5535514/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:44:48  来源:igfitidea点击:

How to fix warning from date() in PHP"

phpdatewarningsxampp

提问by Nano HE

I am using XAMPP(PHP Version 5.3.1) on winxp. When I try to call time() or date() function on my localhost. It will show warning message as this,

我在 winxp 上使用 XAMPP(PHP 版本 5.3.1)。当我尝试在本地主机上调用 time() 或 date() 函数时。它将显示警告消息,如下所示,

Severity: Warning

Message: 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 'UTC' for '8.0/no DST' instead

Filename: helpers/date_helper.php

严重性:警告

消息:date() [function.date]:依赖系统的时区设置是不安全的。您需要使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了这些方法中的任何一种并且仍然收到此警告,则很可能是您拼错了时区标识符。我们为“8.0/无夏令时”选择了“UTC”

文件名:helpers/date_helper.php

How can I disable the warning? Thanks.

如何禁用警告?谢谢。

回答by Alex Pliutau

Try to set date.timezonein php.inifile. Or you can manually set it using ini_set()or date_default_timezone_set().

尝试date.timezonephp.ini文件中设置。或者您可以使用ini_set()或手动设置它date_default_timezone_set()

回答by Poelinca Dorin

You need to set the default timezone smth like this :

您需要像这样设置默认时区:

date_default_timezone_set('Europe/Bucharest');

More info about this in http://php.net/manual/en/function.date-default-timezone-set.php

有关此的更多信息http://php.net/manual/en/function.date-default-timezone-set.php

Or you could use @in front of date to suppress the warning however as the warning states it's not safe to rely on the servers default timezone

或者您可以@在日期前使用来抑制警告,但是警告指出依赖服务器默认时区是不安全的

回答by Sumeet Basak

You could also use this:

你也可以使用这个:

ini_alter('date.timezone','Asia/Calcutta');

You should call this before calling any date function. It accepts the key as the first parameter to alter PHP settings during runtime and the second parameter is the value.

您应该在调用任何日期函数之前调用它。它接受键作为在运行时更改 PHP 设置的第一个参数,第二个参数是值。

I had done these things before I figured out this:

在我弄明白这个之前,我已经做了这些事情:

  1. Changed the PHP.timezone to "Asia/Calcutta" - but did not work
  2. Changed the lat and long parameters in the ini - did not work
  3. Used date_default_timezone_set("Asia/Calcutta");- did not work
  4. Used ini_alter()- IT WORKED
  5. Commented date_default_timezone_set("Asia/Calcutta");- IT WORKED
  6. Reverted the changes made to the PHP.ini - IT WORKED
  1. 将 PHP.timezone 更改为“Asia/Calcutta” - 但没有用
  2. 更改了 ini 中的 lat 和 long 参数 - 不起作用
  3. 使用过date_default_timezone_set("Asia/Calcutta");- 没有用
  4. 使用过ini_alter()- 有效
  5. 评论date_default_timezone_set("Asia/Calcutta");- 它有效
  6. 恢复对 PHP.ini 所做的更改 - 它有效

For me the init_alter()method got it all working.

对我来说,这个init_alter()方法让一切都奏效了。

I am running Apache 2 (pre-installed), PHP 5.3 on OSX mountain lion

我在 OSX 山狮上运行 Apache 2(预装)、PHP 5.3

回答by cottton

This just happen to me because in the php.ini the date.timezone was not set!

这只是发生在我身上,因为 在 php.ini 中 date.timezone 没有设置!

;date.timezone=Europe/Berlin

;date.timezone=欧洲/柏林

Using the php date()function triggered that warning.

使用 php date()函数触发了该警告。

回答by Dejan Marjanovic

error_reporting(E_ALL ^ E_WARNING);

:)

:)

You should change subject to "How to fix warning from date() in PHP"...

您应该将主题更改为“如何在 PHP 中修复来自 date() 的警告”...