php - 使用 PHP 的 DateTime 警告

标签 php datetime date

<分区>

我有这个代码:

try{
  $datetime1 = new DateTime("now");
  $datetime2 = new DateTime("now");
  $interval = $datetime1->diff($datetime2);
  var_dump($interval);
}
catch(Exception $e) {
  echo $e->getMessage();
}     

我没有看到关于 var_dump 的任何结果,php 的输出是:

DateTime::__construct(): It is not safe to rely on the system's timezone setting
s. You are *required* to use the date.timezone setting or the date_default_timez
one_set() function. In case you used any of those methods and you are still gett
ing this warning, you most likely misspelled the timezone identifier. We selecte
d 'Europe/Paris' for '1.0/no DST' instead

我正在使用 php_cli 5.3.8

我能做什么? 谢谢

最佳答案

在PHP5.3中,需要设置时区。

打开 php.ini 并找到设置 date.timezone。取消注释并将其设置为某个值,例如

date.timezone = "Europe/Paris"

然后重启服务器。它应该处理警告。

[编辑]

@greut 提出的解决方案同样有效。两者之间的区别在于 php.ini 设置是服务器范围的(将应用于现在和将来在服务器上运行的所有应用程序),而 date_default_timezone_set() 应用于正在运行的应用程序目前仅执行。

php.ini 解决方案可让您忘记您运行的任何其他站点上的警告 - 但您需要访问 php.ini。使用 date_default_timezone_set() 将覆盖 php.ini 中的设置,因此您可以根据需要同时使用这两种方法。

关于php - 使用 PHP 的 DateTime 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835136/

相关文章:

c# - JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同

java - 有没有另一种方法可以在 Android 日历选择器中添加天数

android - 如何检查所选日期比android中的当前日期多8天

php - Symfony 信使队列与绑定(bind)键 - 重试策略

php - 如何防止用户为网址添加书签?

php - javascript中的ie8 split函数问题

php - 从另一个 DateInterval 中减去一个 DateInterval

java - PHP 与 Java 是否存在能耗差异?

c# - 日期时间插入错误

perl - 如何在 Perl 中将 ISO8601 时间戳转换为 unix 时间?