WordPress Begin主题文章于8小时前发表的解决办法

2016年4月6日08:22:42网站建设3971

最近在群里经常看到有朋友反馈Begin主题文章发布时间提前8个小时的问题,我自己也查了下我的站点,发现刚发表的文章,在Begin主题文章页面的“版权声明”处显示发表于8小时前。文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

Wordpress Begin主题文章发布时间8小时前的解决办法文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

对服务器配置的时区和Wordpress设置里的时区进行了检查,并未发现任何问题,两个地方配置的都是东8区。最后对begin主题进行了检查,begin主题的inc/copyright.php文件中通过以下语句计算文章的发表时间:文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

  1. <?php echo timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))); ?>

timeago函数的实现方法如下:文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

  1. // 时间
  2. function timeago($ptime)
  3. {
  4.     $ptime = strtotime($ptime);
  5.     $etime = time() - $ptime;
  6.     if ($etime < 1) {
  7.         return '刚刚';
  8.     }
  9.     $interval = array(
  10.         12 * 30 * 24 * 60 * 60 => '年前',
  11.         30 * 24 * 60 * 60 => '个月前',
  12.         7 * 24 * 60 * 60 => '周前',
  13.         24 * 60 * 60 => '天前',
  14.         60 * 60 => '小时前',
  15.         60 => '分钟前',
  16.         1 => '秒前'
  17.     );
  18.     foreach ($interval as $secs => $str) {
  19.         $d = $etime / $secs;
  20.         if (1 <= $d) {
  21.             $r = round($d);
  22.             return $r . $str;
  23.         }
  24.     }
  25. }

分析以上代码发现,begin主题在此处使用当前系统时间减去文章发表的GMT时间。由于系统时间的为东8区时间,比格林尼治平均时(GMT)快8个小时,所以文章发表后会显示于8小时前发表。文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

解决办法:文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

直接使用当前系统时间减去文章发表的当地时间,打开begin主题inc/copyright.php文件,将里面3处文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

  1. <?php echo timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))); ?>

替换为:文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

  1. <?php echo timeago(get_the_time('Y-m-d G:i:s')); ?>

备注:以上方法适用于已将Wordpress时区修改为上海或UTC+8的用户。文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

2017年9月13日更新:文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

以上为旧版begin主题解决显示文章8小时前发布的方法,最新版begin主题请查看:文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

begin主题显示文章8小时前发布的解决办法文章源自堕落的鱼-https://www.duoluodeyu.com/2327.html

匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

评论:3   其中:访客  2   博主  1
    • Koolight
      Koolight 0

      我的貌似没出现这种情况。

        • 堕落的鱼
          堕落的鱼

          @ Koolight 恩,可能是我们环境配置的不一样。

            • 测试
              测试 0

              @ 堕落的鱼 谢谢大佬,百度了半天,还是你的有用,其他都是转载,我的比较奇葩,首页正常,内容页加8了