在wordpress中获取已发布帖子的时间

时间:2017-03-11 09:17:47

标签: php wordpress datetime timestamp

我的网站上有一个博客。在博客列表中,我想显示日期和时间。我使用the_time()获取发布的发布日期。但是,获得时间是一个棘手的部分。

我不想显示发布的时间,例如1:30 PM

我想显示帖子的持续时间...我的意思是这样的

53 minutes ago 要么 4 hours ago 要么 2 days ago 要么 5 months ago 要么 1 year ago

我首先尝试使用此代码get_post_time('U', $post->ID)get_post_time('U', true),但它会给我时间戳,例如1486128075

并尝试此get_the_time(),但它会给我1:00 PM

希望你理解我的问题

1 个答案:

答案 0 :(得分:0)

在functions.php中添加此代码

function ash_relative_time() { 
$post_date = get_the_time('U');
$delta = time() - $post_date;
if ( $delta < 60 ) {
    echo 'Just Now';
}
elseif ($delta > 60 && $delta < 120){
    echo '1 minute ago';
}
elseif ($delta > 120 && $delta < (60*60)){
    echo strval(round(($delta/60),0)), ' minutes ago';
}
elseif ($delta > (60*60) && $delta < (120*60)){
    echo 'About an hour ago';
}
elseif ($delta > (120*60) && $delta < (24*60*60)){
    echo strval(round(($delta/3600),0)), ' hours ago';
}
else {
    echo the_time('j\<\s\u\p\>S\<\/\s\u\p\> M y g:i a');
}}

然后使用echo ash_relative_time();进行打印。