【发布时间】:2018-01-02 11:38:24
【问题描述】:
我正在使用 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();
}
}
【问题讨论】:
标签: php html wordpress function date