如何比较基于年份的发布帖子?

How to compare publish posts based on years?

我的代码需要帮助。我从“https://wordpress.stackexchange.com/questions/36184/how-can-i-put-posted-x-minutes-ago-on-my-posts”中找到了这段关于如何将发布日期显示为 xx seconds/hours 前的代码。我在比较已发布 post 的年份时遇到问题,如果已发布的年份不匹配,它会显示年份。

function k99_relative_time() { 
$post_date = get_the_time('U');
$delta = time() - $post_date;
if ( $delta < 60 ) {
    echo strval(round(($delta))), ' seconds ago';
}
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 '1 hour ago';
}
elseif ($delta > (120*60) && $delta < (24*60*60)){
    echo strval(round(($delta/3600),0)), ' hours ago';
}
else {
    // code with problems
    // show year in publish date if the years of post don't match
    if (get_the_time('Y') !== get_the_time('Y')) {
        echo the_time('M. d, Y');
    // don't show year if they do match
    } else {
        echo the_time('M. d');
    }
}
}

您有以下行:

if (get_the_time('Y') !== get_the_time('Y')) {

如果比较2个相等的值。 所以那些永远不会不同。

你的意思是下面这样的吗?

if (get_the_time('Y') !== date('Y')) {