新 Web 服务器上的 stroftime 代码未显示日期


stroftime code not displaying date on new web server

我创建了一个博客并将其设置为在每篇博客文章中显示发布日期,我一直使用 stroftime() 并从存储帖子的 MySQL 数据库中获取记录的日期。

这一直有效,但我最近刚刚切换到一个新的网络主机,当我尝试使用原始代码时:

<?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() 替换。

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

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

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

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

基本上,在调用任何日期时间函数之前,您需要在代码中的某个位置调用函数 date_default_timezone_set($timezone)。 您应该始终查看 php 文档页面以获取此类内容。

您需要在代码中使用您所在位置的正确时区调用它,然后调用代码。 您将在 php 文档网站上找到时区列表

这里

例如,对于我所做的所有代码。

 date_default_timezone_set('Europe/Dublin');
 strtotime ('yesterday 12:30 pm');

这只需要在代码中完成一次,因此您可以将其放在配置文件或所有脚本加载的内容中。