【问题标题】:stroftime code not displaying date on new web server [duplicate]stroftime 代码未在新 Web 服务器上显示日期 [重复]
【发布时间】:2016-04-22 12:52:54
【问题描述】:

我创建了一个博客并进行了设置,以便在每篇博客文章中显示该文章的发布日期,我总是使用 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 mysql database mysqli strtotime


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        基本上,在调用任何日期时间函数之前,您需要在代码中的某处调用函数date_default_timezone_set($timezone)。您应该经常查看 php 文档页面以了解此类内容。

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

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

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

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

        【讨论】:

          猜你喜欢
          • 2013-10-02
          • 2019-12-10
          • 1970-01-01
          • 1970-01-01
          • 2012-03-24
          • 2018-03-23
          • 2016-09-19
          • 2014-08-27
          • 2018-01-31
          相关资源
          最近更新 更多