Home > Mobile >  How to compare publish posts based on years?
How to compare publish posts based on years?

Time:02-16

I need help with my code. I found this code from "https://wordpress.stackexchange.com/questions/36184/how-can-i-put-posted-x-minutes-ago-on-my-posts" on how to display the publish date as xx seconds/hours ago. I'm having problems comparing years on a published post where it shows the year if the published years don't match.

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');
    }
}
}

CodePudding user response:

You have the following line:

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

If compares 2 equal values. So those will never differ.

Did you mean something like the following?

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