381

当我请求将服务器上的PHP版本从 5.2.17 更新到 PHP 5.3.21 时,出现此错误。

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  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 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 86</p>

</div>
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 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 86

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 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 99
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  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 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 99</p>

</div>
4

25 回答 25

582

您可能需要将时区放在php.ini文件的配置行中。您的 php.ini 文件中应该有这样的块:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York

如果没有,请添加它(用您的时区替换时区)。配置完成后,一定要重启httpd( service httpd restart)。

这是支持的时区列表

于 2013-05-27T01:14:23.480 回答
209

如果您无法修改 php.ini 配置,您也可以在代码开头使用以下代码段:

date_default_timezone_set('Africa/Lagos');//or change to whatever timezone you want

时区列表可以在http://www.php.net/manual/en/timezones.php找到。

于 2013-08-04T22:54:15.540 回答
75

index.php在您的文件中添加以下内容。当我将我的应用程序从我的XAMPP服务器移动到 Apache 2.2 和 PHP 5.4 时,我第一次遇到了这个问题......

我建议您在index.php文件中而不是在php.ini文件中执行此操作。

if( ! ini_get('date.timezone') )
{
    date_default_timezone_set('GMT');
}
于 2013-11-12T06:30:37.953 回答
18
<? print(gmdate("Y")); ?> 

代替

<? print(date("Y")); ?>

为我工作(显示当前年份,不再显示错误消息)。(感谢上面的克里斯)

于 2014-01-02T21:09:22.547 回答
16

如果这些不是您的选择

  • 修改php.ini.
  • 添加date_default_timezone通话。

而不是date你可以使用gmdate.

gmdate( "Y" )当我需要一年的版权片段时,我就使用过。

于 2013-11-05T14:33:12.883 回答
15

如果您使用 CodeIgniter 并且无法更改 php.ini,我在 index.php 的开头添加了以下内容:

date_default_timezone_set('GMT');
于 2015-11-20T08:40:56.330 回答
14

我总是将此行保留在 codeigniter 的根目录index.php中。这样我的代码就可以在任何服务器上运行

date_default_timezone_set('Asia/Dhaka');

此处支持的时区列表

于 2015-01-10T17:45:04.973 回答
11

@Justis 为我指出了正确的方向,但他的代码对我不起作用。这做到了:

// set the default timezone if not set at php.ini
if (!date_default_timezone_get('date.timezone')) {
    // insert here the default timezone
    date_default_timezone_set('America/New_York');
}

文档:http ://www.php.net/manual/en/function.date-default-timezone-get.php

此解决方案不仅适用于没有完整系统访问权限的用户。当您将任何脚本提供给除您之外的任何人时,它都是必需的。当您将脚本分发给其他人时,您永远不知道该脚本将在哪个服务器上运行。

于 2014-07-02T07:10:05.167 回答
5

我不得不把它放在双引号中。

date_default_timezone_set("America/Los_Angeles"); // default time zone
于 2014-07-22T10:09:12.390 回答
5

这个问题一直困扰着我一段时间,因为我试图将“createbucket.php”脚本注入作曲家,但我一直被告知我的时区不正确。

最后,解决问题的唯一方法是: $ sudo nano /etc/php.ini

搜索时区

[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = UTC

确保删除;

然后终于

$ sudo service httpd restart

你会很高兴去的:)

于 2016-08-16T06:13:31.923 回答
4

您可以在.htaccess文件中设置时区

php_value date.timezone UTC
于 2018-01-18T21:41:20.003 回答
2

除了在几个答案中提到设置 date.timezone= 之外,我还在 php.ini 文件中发现了一个错误,导致它无法到达 date.timezone。我发现它的方法是在终端的命令行中运行 php。这导致在第 114 行报告错误。在我的情况下,我取消了显示错误的设置的注释“|” 2个值之间。它不喜欢它。我删除了其中一个值和 | 之后一切都很好

于 2015-02-04T02:44:51.640 回答
2
<? date_default_timezone_set('Europe/Istanbul'); ?>

对于 php(或您的位置)。

于 2019-01-18T23:11:27.660 回答
1

两个时区的简单方法。

<?php 
$date = new DateTime("2012-07-05 16:43:21", new DateTimeZone('Europe/Paris')); 

date_default_timezone_set('America/New_York'); 

echo date("Y-m-d h:iA", $date->format('U')); 

// 2012-07-05 10:43AM 
?>
于 2015-08-18T04:07:22.417 回答
1

对于 docker 用户:在您的项目中创建一个本地 timezone.ini 文件,在 docker-compose.yml 中设置配置,

    volumes:
        - "./docker/config/timezone.ini:/usr/local/etc/php/conf.d/timezone.ini"

时区.ini

    date.timezone=Australia/Sydney
于 2018-05-25T00:04:06.700 回答
0

纠正不兼容性时的快速解决方案是禁用index.php文件中的错误报告:

将下面的行插入下面的index.phpdefine( ‘_JEXEC’, 1 );

error_reporting( E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR |
E_COMPILE_WARNING );
于 2014-09-04T12:23:49.340 回答
0

我在 us-west-2(俄勒冈)地区托管我的 EC2 和 S3 存储桶。当我调用$s3client->listBuckets()在我的 php 中列出现有存储桶时,我遇到了异常 -"Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings...". 我进行了以下更改以使其工作。分享这些详细信息以防有人面临类似问题并且上述任何答案都没有帮助。

  1. 根据文档@AWS 配置网络时间协议 (NTP)ntpstat ,我通过运行命令确认 ntpd 服务状态正常。如果您遇到错误,此链接可帮助您了解问题所在。
  2. 打开 /etc/php.ini 文件(根据安装的 php 版本,有些可能有不同的路径),发现它date.timezone没有设置为任何值,并且默认情况下它被注释了。我通过删除';'取消评论 在此行之前并将其值设置为"UTC"并保存文件。
  3. sudo service httpd restart使用和sudo service ntpd restart命令重新启动 http 和 ntp 守护程序。

在此之后,我能够成功列出存储桶,没有任何例外。希望这可以帮助。

于 2016-09-26T07:49:22.730 回答
0

如果您使用 Plesk,请尝试一下,首先,打开 PHP 设置,在页面底部,将 date.timezone 从 DEFAULT 更改为 UTC。

在此处输入图像描述

在此处输入图像描述

于 2016-12-18T18:58:38.343 回答
0

我在 chroot 监狱中运行 php-fpm 时遇到此错误。我尝试在 chroot 目录中创建 etc/php.ini 和 /usr/share/zoneinfo,但它不起作用。我什至尝试对 php-fpm 守护进程进行 strace 以查看它们丢失了什么文件 - 没有任何东西跳出来。

因此,如果 Google 将您带到这里,因为您在使用为 chroot 配置的 php-fpm 时遇到此错误,您可以通过/etc/php-fpm.d/www.conf在 ENV 部分添加以下行来修复它:

env[TZ] = America/New_York

通常需要重新启动 php-fpm 才能使其生效。希望这可以帮助那里的人。

于 2017-03-12T02:01:35.503 回答
0

在我的特殊情况下,我将 PHP 配置为使用 PHP-FPM(FastCGI 进程管理器)。从 CLI执行phpinfo()时,我看到了我在 php.ini 中设置的正确时区,但是它在浏览器中仍然不正确并导致我的代码失败。我只需要重新启动php-fpm服务器上的服务。

service rh-php56-php-fpm restart

如果您httpd编辑了php.ini

service httpd restart
于 2017-07-26T02:37:10.820 回答
0

上面来自CtrlX的这个答案是正确的答案,但它可能无法完全发挥作用。我将此行添加到我的php.ini文件中:

date.timezone = "America/Los_Angeles"

但它并没有删除我所有文件的 PHP 错误,因为我的一些 PHP 脚本位于子文件夹中。所以我必须编辑.htaccess文件来设置 php.ini 以递归使用(在子文件夹中):

suphp_configpath /home/account_name/public_html

其中 account_name 是您的 cpanel 帐户名称,public_html 是您的 php.ini 文件所在的文件夹。

于 2017-08-03T21:56:58.393 回答
0

如果您无权访问该文件php.ini,请在您的域或子目录的根目录中创建或编辑.htaccess文件并添加此文件(由 cpanel 生成):

<IfModule mime_module>
AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>

<IfModule php5_module>
php_value date.timezone "America/New_York"
</IfModule>

<IfModule lsapi_module>
php_value date.timezone "America/New_York"
</IfModule>
于 2019-07-25T01:40:00.837 回答
0

解决这个问题有两种选择

首先,更改为 php.ini 文件并输入默认时区

date.timezone = "America/New_York"

一次,您在php.ini中设置了时区,重新启动服务器

其次,根据您的需要更改运行时分配时区

date_default_timezone_set('America/New_York');
于 2020-03-05T07:44:06.703 回答
0

我已经在 .htaccess 文件中完成或创建了这个:

php_value date.timezone Asia/Jakarta

但它不起作用。然后我尝试将php版本从5升级到7。它解决了这个问题。

于 2021-07-10T10:52:09.350 回答
-1

在您的连接页面中,date_default_timezone_set("Africa/Johannesburg");根据您当前的位置放置这样的代码。

于 2020-05-09T22:06:08.410 回答