2012-01-12 89 views
3

日期时间我有这样的代码:警告使用PHP

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

我该怎么办? 感谢

回答

12

在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中的设置,所以如果您愿意,可以同时使用两种方法。

+0

错误消息提示通过编程解决方案。加上一个用于暗示不涉及代码修改的解决方案,这将导致更多的可移植代码。 – davogotland 2012-01-12 12:43:35