WordPress.org

Support

Support » Themes and Templates » How to update "Time Ago" function?

How to update "Time Ago" function?

  • I came across this article http://techably.com/dynamic-wordpress-time-format/3693/.

    It works nicely, so far it displays the time as “Posted 30 minutes ago”, cool.

    The issue is that I want it to say “Just now” if it was published less than 30 seconds to 1 minute ago.
    I’d also want to display hours, days, weeks, months, and years ago. But this function is very limited, after a certain amount of time the regular date is displayed.

    How can I tweak the code up a bit to accomplish this?…

Viewing 8 replies - 1 through 8 (of 8 total)
  • I haven’t really tested it, but I think this should it:

    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 > 60){
         $mytimestamp = __('Just now');
       }else{
         $mytimestamp = sprintf(__('%s ago'), human_time_diff($time));
       }
       return $mytimestamp;
     }

    No, now all of the dates say Just now 🙁

    Sorry, I made a mistake, it should be if($mytime < 6){, as shown below:

    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 < 60){
         $mytimestamp = __('Just now');
       }else{
         $mytimestamp = sprintf(__('%s ago'), human_time_diff($time));
       }
       return $mytimestamp;
     }

    Works nicely! Thanks!
    Can you help be display “1 Month ago” after 4 weeks has past and “2 Months ago” along with years ago?

    Quick question does the updated time code display “Weeks ago”?

    Moderator Jan Dembowski

    @jdembowski

    Volunteer Mod. & Brute Squad

    I’ve not visited it recently but I think this post may be what you are looking for.

    http://www.jasonbobich.com/wordpress/a-better-way-to-add-time-ago-to-your-wordpress-theme/

    It was an answer from an earlier post. 😉

    http://wordpress.org/support/topic/human_time_diff-for-years-ago?replies=3

    Edit: Not sure it does weeks though, but that post is a good start.

    Jan, I tried that before 🙂 The notes say “Where you see ‘themeblvd’ below, you’d want to replace those with whatever term you’re using in your theme to provide support for localization.” But I have no idea what that is or how to do it…
    So I tried looking for another technique.
    Can you help me with that?

    Jan, I just tried using that technique the most recent post now says “sometime” Why is that happening? Do You know?…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to update "Time Ago" function?’ is closed to new replies.