2017-07-26 303 views
1

我使用WordPress主题报纸。现在日期显示为“2017年7月26日”,但我希望它显示为“4小时前”或“3天前”。环顾四周后,我发现有一个WordPress代码可以做到这一点。但是,我无法弄清楚如何用人类时间差异替换我网站的当前日期。有没有人有什么建议?如何将日期更改为前几天(人类时差)

的获取日期函数我的主题使用:

function get_date($show_stars_on_review = true) { 
     $visibility_class = ''; 
     if (td_util::get_option('tds_m_show_date') == 'hide') { 
      $visibility_class = ' td-visibility-hidden'; 
     } 

     $buffy = ''; 
     if ($this->is_review and $show_stars_on_review === true) { 
      //if review show stars 
      $buffy .= '<div class="entry-review-stars">'; 
      $buffy .= td_review::render_stars($this->td_review); 
      $buffy .= '</div>'; 

     } else { 
      if (td_util::get_option('tds_m_show_date') != 'hide') { 
       $td_article_date_unix = get_the_time('U', $this->post->ID); 
       $buffy .= '<span class="td-post-date">'; 
        $buffy .= '<time class="entry-date updated td-module-date' . $visibility_class . '" datetime="' . date(DATE_W3C, $td_article_date_unix) . '" >' . get_the_time(get_option('date_format'), $this->post->ID) . '</time>'; 
       $buffy .= '</span>'; 
      } 
     } 

     return $buffy; 
    } 

当函数被调用:

<div class="item-details"> 
     <?php echo $this->get_title();?> 
     <div class="td-module-meta-info"> 
      <?php if (td_util::get_option('tds_category_module_mx1') == 'yes') { echo $this->get_category(); }?> 
      <span class="td-author-date"> 
       <?php echo $this->get_author();?> 
       <?php echo $this->get_date();?> 
      </span> 
     </div> 
     <div class="td-excerpt"> 
      <?php echo $this->get_excerpt();?> 
     </div> 
    </div> 

人的时间差异代码:

<?php echo str_replace('mins', 'minutes', human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'); ?> 

我在哪里元素试图更改日期

<?php 
 

 
class td_module_2 extends td_module { 
 

 
    function __construct($post) { 
 
     //run the parrent constructor 
 
     parent::__construct($post); 
 
    } 
 

 
    function render() { 
 
     ob_start(); 
 
     ?> 
 

 
     <div class="<?php echo $this->get_module_classes();?>"> 
 
      <div class="td-module-image"> 
 
       <?php echo $this->get_image('td_324x160');?> 
 
       <?php if (td_util::get_option('tds_category_module_2') == 'yes') { echo $this->get_category(); }?> 
 
      </div> 
 
      <?php echo $this->get_title();?> 
 

 

 
      <div class="td-module-meta-info"> 
 
       <?php echo $this->get_author();?> 
 
       <?php echo wp_relative_date(get_the_time('U'));?> 
 
      </div> 
 

 

 
      <div class="td-excerpt"> 
 
       <?php echo $this->get_excerpt();?> 
 
      </div> 
 

 
      <?php echo $this->get_quotes_on_blocks();?> 
 

 
     </div> 
 

 
     <?php return ob_get_clean(); 
 
    } 
 
}

回答

0

下面是一些代码,转换为以分钟,小时,或根据设定​​的当前日期和时间与发布日期是在创建元素的日期戳天。

请尝试看看这是否符合您的要求。

更改$ post_date创建您的文章的时间戳。 (EG在下面的例子中,根据我发布这个答案的时间显示为1天...)

将下面的代码包装在一个函数中,并将带有时间戳的wordpress getDate特性('7/25/2017 00:00:00'),该功能将返回小时,分钟,天。

$seconds_hours = 60*60; 
$hours_days = $seconds_hours*24; 

$today = date("Y-m-d"); 
$timestamp = $today.' '.date("H:i:s"); 

$post_date = '7/25/2017 00:00:00'; 

$difference = strtotime($timestamp)-strtotime($post_date); 
if ($difference>$hours_days) { 
    $date1 = new DateTime(substr($post_date,0,10)); 
    $date2 = new DateTime($today); 
    $since = $date2->diff($date1)->format("%d"); 
    if ($since==1) { $since .= ' day'; } 
    else { $since .= ' days'; } 
} else if ($difference>$seconds_hours) { 
    $since = floor($difference/$seconds_hours).' hours'; 
} else { 
    $since = floor($difference/60).' mins';//mins 
} 

echo $since; 

WordPress的解决方案(未测试)

function human_difference($post_date) { 
    $seconds_hours = 60*60; 
    $hours_days = $seconds_hours*24; 

    $today = date("Y-m-d"); 
    $timestamp = $today.' '.date("H:i:s"); 

    $difference = strtotime($timestamp)-strtotime($post_date); 
    if ($difference>$hours_days) { 
     $date1 = new DateTime(substr($post_date,0,10)); 
     $date2 = new DateTime($today); 
     $since = $date2->diff($date1)->format("%d"); 
     if ($since==1) { $since .= ' day'; } 
     else { $since .= ' days'; } 
    } else if ($difference>$seconds_hours) { 
     $since = floor($difference/$seconds_hours).' hours'; 
    } else { 
     $since = floor($difference/60).' mins';//mins 
    } 

    return $since; 
} 

$post_date_time = get_the_date() . ' ' . get_the_time(); //Format this to m/d/Y H:i:s 
echo human_difference($post_date_time); // x days or x hours or x minutes 
+0

感谢试图帮助。我得到了这个错误 - 解析错误:语法错误,意外'$ post_date_time'(T_VARIABLE),期待函数(T_FUNCTION)在这一行 - $ post_date_time = get_the_date()。 ''。 get_the_time(); //格式化为m/d/Y H:i:s – user7548188

+0

确保将正确的格式添加到get_the_date和time中。 EG get_the_date('m/d/Y')等 –

0

在functions.php文件添加

function wp_relative_date($post_date) 
{ 
    return human_time_diff($post_date , current_time('timestamp')) . ' ago'; 
} 

要显示的时间内环路添加此

echo wp_relative_date(get_the_time('U')); 
+0

我将如何专门添加到我的主题?我有上面的代码(在项目细节类中) – user7548188

+0

您可以简单地将第一部分放在functions.php文件的类之外。它是在一个类或命名空间下? – Webkix

+0

我在我的答案中放置了元素的代码,我在上面更改了日期。它看起来不错吗?这是说所有日期都是6天前,即使它们是10天前或10小时前? – user7548188

相关问题