0

Right now I've got future posts displayed on my site, and I was using this code on each post to show when it was published.

<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>

Naturally it shows a post 2 days from now as 2 days ago. How might I fix it so it says "ago" and maybe "from now" where applicable?

  • Have you tried changing the word ago to from now? That word isn't a part of the function it's just added on to the end – Tom J Nowell Jul 13 '17 at 19:53
  • Not all my posts are in the future though, so then older posts would say from now as well. – mwz Jul 13 '17 at 19:56
1

You could also do it using human_time_diff hook, but it would make it global:

function my_time_diff_mins( $since, $diff, $from, $to ) {
   return $since . ($from > $to ? ' from now' : ' ago');
}
add_filter( 'human_time_diff', 'my_time_diff_mins', 10, 4 );
0

I managed to figure it out

<?php
 if ( get_post_status ( $ID ) == 'future' ) { 
echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' from 
now';
 } else { 
echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.