2016-01-17 45 views
2

我已经创建了一个博客并设置了它,以便在每个博客文章中显示该博文的发布日期,我总是使用stroftime()并获取记录来自存储帖子的MySQL数据库的日期。stroftime代码不会在新的web服务器上显示日期

这一直工作,但我刚刚换了新的虚拟主机,当我尝试使用原来的代码:

<?php $date = strtotime($post['post_date']); 
     echo date('F jS, Y', $date); ?> 

我得到一个错误,指出:

Warning: strtotime(): 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. 

我米只是不明白他们希望我用date_default_timezone_set()替换。

回答

1

Php预计默认时区由应用程序而不是由系统设置。因此,您必须在脚本的开始或初始化的一部分中调用date_default_timezone_set sonewhere。然后你可以安全地调用strtotime。

date_default_timezone_set('UTC') 
... 
strtotime (...) 
1

你可以在php.ini文件中配置它,如:

date.timezone = "US/Central" // US/Central is your default timezone 
1

基本上你需要调用的函数date_default_timezone_set($timezone)某处你的代码中调用任何日期时间函数之前。你应该总是看看这个php文档页面。

您需要在您的代码中使用正确的时区来调用此位置,然后调用您的代码。 YOu会在php文档中找到时区的列表website here

例如对于我所有的代码。

date_default_timezone_set('Europe/Dublin'); 

strtotime ('yesterday 12:30 pm'); 

此只需要在你的代码做一次,所以你可以把它放在一个配置文件,或者是由所有的脚本加载的东西。