Implementing the Time Ago

Posted in: Newspaper
Post count: 10

Hello Forum,

I have a website:
https://happeningnownews.com

I want to implement the time ago functionality on my website but its not working.
I have used 2 plugins, i have added the code

add_filter(‘the_time’, ‘dynamictime’);
function dynamictime() {
global $post;
$date = $post->post_date;
$time = get_post_time(‘G’, true, $post);
$mytime = time() – $time;
if($mytime > 0 && $mytime < 7*24*60*60)
$mytimestamp = sprintf(__(‘%s ago’), human_time_diff($time));
else
$mytimestamp = date(get_option(‘date_format’), strtotime($date));
return $mytimestamp;
}

to the very end of my theme file function.php but nothing seems to be working.

Can I get a fix to this please?

Regards

Post count: 20685

Hi,

Such plugins will not work, this will require modifications in the theme files. It also depends on where you want to display the time ago, in modules or in the post page.

In the post page it is possible with cloud post templates. The date can be displayed in a time ago format (for a 7 day period)

You can test this by previewing then editing a cloud post template here
https://affiliate.tagdiv.com/#/load/Single
https://www.screencast.com/t/5jK9ApgeYCN

In modules it could only be done with some modifications
https://forum.tagdiv.com/topic/the-time-ago-in-block-3-is-correct-but-another-block-is-wrong-time/

Thanks

Post count: 10

Hello,

Thanks for the reply.

I used the code:
<?php echo human_time_diff( get_the_time(‘U’, $this->post->ID), current_time(‘timestamp’) ). ‘ ago’; ?>

on the modules and it seems to be working but for post older than 7 days, its still showing months and even years ago.

I have tried using the strtotime() function on it but there was nothing returned.

How can I only show the time ago if the post is not older than 7 days?

Post count: 20685
Post count: 10

Hello,

I noticed that the function “$this->get_date()” returns a lot of CSS and HTML like this:
“<span class=”td-post-date”><time class=”entry-date updated td-module-date” datetime=”2018-12-14T17:33:41+00:00″ >14th Dec. 2018 @ 05:33 pm</time></span>”

which made the function strtotime( $this->get_date() ) fail.

The Solution.
Using the function “get_the_time(‘U’, $this->post->ID)”, I was able to get the timestamp of the post and using the code below, i was able to achieve the question I asked.

<?php echo ( get_the_time(‘U’, $this->post->ID) > strtotime( “5 days ago” ))? human_time_diff( get_the_time(‘U’, $this->post->ID), current_time(‘timestamp’) ). ‘ ago’ : $this->get_date(); ?>

for post less than 5 days ago, it will display the “ago”, while if the post is more than 5 days ago, it will display the date

Try and see if it works for you.

Post count: 20685

Hi,

It works for me just as you describe it. Posts older than 5 days will display the default date, while the others display a time ago format.

Thank you for taking the time to post post the solution, it may be helpful for other users.

Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic.