WordPress.org

Ready to get started?Download WordPress

Forums

How to update "Time Ago" function? (9 posts)

  1. keysuuh
    Member
    Posted 1 year ago #

    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?...

  2. tschoffelen
    Member
    Posted 1 year ago #

    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;
     }
  3. keysuuh
    Member
    Posted 1 year ago #

    No, now all of the dates say Just now :(

  4. tschoffelen
    Member
    Posted 1 year ago #

    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;
     }
  5. keysuuh
    Member
    Posted 1 year ago #

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

  6. keysuuh
    Member
    Posted 1 year ago #

    Quick question does the updated time code display "Weeks ago"?

  7. 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.

  8. keysuuh
    Member
    Posted 1 year ago #

    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?

  9. keysuuh
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic