【问题标题】:Warning with DateTime using PHP [duplicate]使用 PHP 的 DateTime 警告 [重复]
【发布时间】:2012-02-08 17:53:21
【问题描述】:

我有这个代码:

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

我能做什么? 谢谢

【问题讨论】:

    标签: php datetime date


    【解决方案1】:

    在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 中的设置,因此您可以根据需要同时使用这两种方法。

    【讨论】:

    • 错误信息提示编程解决方案。加一个用于建议不涉及代码修改的解决方案,这将导致更可移植的代码。
    【解决方案2】:

    在您的代码中执行此操作:

    date_default_timezone_set('Europe/Paris');

    或在 php.ini 中。

    但更好的是,阅读文档:http://php.net/date_default_timezone_set

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 1970-01-01
      • 2012-10-05
      • 2014-07-20
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2012-12-14
      • 2018-09-13
      相关资源
      最近更新 更多