How to fix warning from date() in PHP" [duplicate]

2019-01-11 00:31发布

This question already has an answer here:

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,

Severity: Warning

Message: date() [function.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 'UTC' for '8.0/no DST' instead

Filename: helpers/date_helper.php

How can I disable the warning? Thanks.

5条回答
不美不萌又怎样
2楼-- · 2019-01-11 01:15
error_reporting(E_ALL ^ E_WARNING);

:)

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-11 01:17

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

;date.timezone=Europe/Berlin

Using the php date() function triggered that warning.

查看更多
够拽才男人
4楼-- · 2019-01-11 01:21

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

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

查看更多
老娘就宠你
5楼-- · 2019-01-11 01:25

Try to set date.timezone in php.ini file. Or you can manually set it using ini_set() or date_default_timezone_set().

查看更多
Ridiculous、
6楼-- · 2019-01-11 01:27

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.

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

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

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

查看更多
登录 后发表回答