Skip Navigation

[Résolu] Time Ago Reference

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 Il y a 6 années et 6 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Marqué : ,

This topic contains 10 réponses, has 2 voix.

Last updated by Shane Il y a 6 années et 6 mois.

Assisted by: Shane.

Auteur
Publications
#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: Anglais (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: Anglais (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: Anglais (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: Anglais (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: Anglais (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: Anglais (English )

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

resolved

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.