PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] How to compare publish posts based on years?

 March 03, 2022     php, wordpress, wordpress-theming     No comments   

Issue

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

Solution

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')) {


Answered By - thephper
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing