Skip Navigation

[Resolved] Time Ago Reference

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here was that the customer wanted to have his published post have the reference "time ago" on his published times. So if a post was publish yesterday it would say "24 hours ago"

Solution:

Add the following shortcode to your functions.php file

add_shortcode('time-ago', 'time_ago_shortcode');
function time_ago_shortcode($atts, $content = null) {
human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
}

Then use it by doing this [time-ago]

This support ticket is created 4 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 10 replies, has 2 voices.

Last updated by Shane 4 years, 12 months ago.

Assigned support staff: Shane.

Author
Posts
#574584
2017-09-28 11_06_15-Edit Themes ‹ Community Wedding of Calvert County — WordPress.jpg
2017-09-28 11_06_06-Edit View ‹ Community Wedding of Calvert County — WordPress.jpg

I am trying to: https://toolset.com/forums/topic/shortcode-for-current-time-modified-date/#post-27061

Link to a page where the issue can be seen: hidden link

I expected to see: The time I actually posted the post.

Instead, I got: 48 years. When it was posted the day I am submitting this ticket.

#574675

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Donald,

Thank you for contacting our support forum.

It seems that you are using the function incorrectly based on the way its meant to be used or rather the parameters that it accepts.

human_time_diff( int $from, int $to = '' )

So you see the first parameter should be the from and the second should be to.

Take a look at the link below for example usage of this code.
https://developer.wordpress.org/reference/functions/human_time_diff/

Thanks,
Shane

#574680

This is simply what the other support ticket said.

Where would I need to change the code to add that?

#574693

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Donald,

I'm imagining that you want to compare a timestamp to our current time.

Is it that you want to do it with the published date or the modified date?

Please let me know.
Thanks,
Shane

#574839

Published date please. There won't be any modifying the post once it is published.

#574895

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Donald,

Try using this.

human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';

Please let me know if it helps
Thanks,
Shane

#574922

Doesn't look like it worked

add_shortcode('time-ago', 'time_ago_shortcode');
function time_ago_shortcode($atts, $content = null) {
human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';
}
#574925

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Donald,

You need to add the return statement as well.

return human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago';

Thanks,
Shane

#574926

Works! Thank you!

#574933

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Donald,

Awesome if there are no further issues then you can mark this ticket as resolved.

Thanks,
Shane

#574967

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

resolved