WordPress Plugin: Time Since

Time Since plugin in actionIf you read Simon Willison’s Weblog, you’re probably familiar with his time_since() PHP function (which was actually written by his girlfriend). This allows him to display the time elapsed since an entry was published. I took this code almost verbatim and created my first WordPress plugin. Here it is:

Time Since Plugin

Download and save the file above as time_since.php into your/wp-content/plugins directory. Then within WordPress, go to the Plugin’s section of your weblog admin and activate the Time Since plugin.

This plugin will wrap any date or time (the_date() or the_time()) with an anchor element containing the elapsed time since the entry was published as a title attribute. This allows a user to hover over a date or time and see the elapsed time.

Update: A few people have reported a problem with getting the exact elapsed time to calculate. I suspect this has something to do with a host server’s time (typically set to the local time) varying from the entry post’s time adjusted for the timezone. It works on this site since my host server’s time is the same as my weblog’s timezone adjustment configuration. I’ll have to find a fix soon. Just know that this is a known issue. Thanks to those who reported it.

39 Responses to “WordPress Plugin: Time Since”

  1. Michael Heilemann Says:

    I had something similar to this several version ago. I’ve now implemented this sucker on my blog.

    It does seem to come up with some strange numbers for new posts though (-1 year, 12 months)…

  2. somefoolwitha.com Says:

    Time Since implemented
    Like Binary Bonsai, Ive added the time since plugin - wave your mouse over the post date and Itll tell you how long since that post. It does have an issue with very recent posts though, and its possibly a big waste of time but hey, if you can, then do.

  3. DarkBlue Says:

    I implemented a Perl version on my blog. No problem whatsoever with new posts using this code.

  4. DarkBlue Says:

    I forgot the link: http://urbanmainframe.com/folders/blog/20040217/page_1.htm

  5. Blogging Pro - Blog News, Tools and Hacks Says:

    WordPress Plugin: Time Since
    Rich Manalang’s first plugin display’s the time since the post was published as a title attribute.

  6. Ozguru Says:

    I am a perl coder (not a php coder) but this is really cool. In the original code (if I read it correctly), it was possible to pass a time (whereas this excellent plugin takes the time from the post). Would it be possible to keep both options available so that the plugin could be used outside the loop as well. I have hacked a variant so that I can put a ‘This blog created on 2003/02/01′ type phrase on the right menu where the date has a ‘time since’ mouse-over. Unfortunately I don’t have the skills (yet) to merge this back into your plugin…..

    (And I can’t show the results yet because they are on my test server not the production blog.)

  7. Blogging Pro - Blog News, Tools and Hacks » WordPress Plugin: Time Since Says:

    [...] 49637); Filed under: WordPress Plugins | 11:33 pm {} Rich Manalang’s first plugin display’s the time since the post was published as a title attri [...]

  8. Alex Says:

    Very nice. Perhaps, you can extend it to allow the use of the tag to show time since instead of the post time.

    P.S.
    Your site looks really odd in Safari. The sidebar appears in the middle of the page and there is a lot of white space before the comments.

  9. Rich Says:

    Alex, thanks for the heads up on Safari. Regarding the plugin, I’ll add another function that will allow you to do this.

  10. Alex Says:

    Rich, everything look great in Safari now ;)

  11. nate Says:

    Unless I’m missing something, I’ve found an issue with your plugin. If the_date() or the_time() use the before and after arguments in the function, like say an

    set of tags, it breaks the validity of the XHTML. I tried making a quick workaround, but kinda got bogged down. If you wouldn’t mind, I’d be happy to use the plugin if it kept the code valid.

  12. Ryan Says:

    Do you have this installed on your site? Because when I hover over your post time nothing shows up.

  13. Rich Manalang Says:

    Ryan, I turned it off a few days ago (debugging a problem)… it’s back on now.

  14. Ryan Says:

    Thanks! Now I know it’s not my PC, I just didnt install it right. :P It’s a really cool plugin.

  15. Ryan Says:

    So how would I install this? I just save the txt file as a php file, put it in my plugins folder and activate it?

  16. Rich Manalang Says:

    Ryan, yep… just save as a php file then activate.

  17. ivari Says:

    That GReAT Plugin

    Thanks

  18. Wayne Colby Says:

    Awesome plugin. Danke!

  19. Henna blogs ... » Time Since Plugin Says:

    [...] to the post date you can see the time since that post… kinda cool! Did this with theTime Since Plugin Comments » [...]

  20. mattmadethis.com » Useful WordPress Plugins? Says:

    [...] ;ll probably use this one. Time Of Day – seems useful, although I’m tempted by Time Since which shows the time since the post was published, 3 days for exam [...]

  21. Gilbert Says:

    Rich, I think I’ve found a solution to the problem caused by the host server’s time differing from the local post time.

    Edit line 23 as follows:


    $today = time() + 60*60*x;

    where x is the time difference between the host server and the entry post’s time in hours. I think there should be an easier way to do this by calculating x automatically using PHP, but unfortunately I’m not very familiar with PHP…please tell me if this is possible.

    In any case the plugin now works fine on my blog.
    cheers!

  22. Rich Says:

    Thanks Gilbert. I’ll look into automating this based on the time offset property that’s stored in your WP options.

    Rich

  23. ryan masuga Says:

    I’ve implemented this on my site as subtle text next to the date of the post. It’s a great plugin, but I am interested in a fix for the “-1 years, 12 months ago” post problem. When I try the $today = time() + 60*60*x; code, it says my last post was 0 minutes ago, which is fine, but then all my previous posts seem to be off by the offset I enter there. It seems anything less than an hour is listed with the -1. It seems like a simple enough fix, but I’m not enough of a PHP jockey to alter it.

    Admittedly, this is a minor problem, but it sure would look nicer to see “12 minutes ago” rather than “-1 years, 12 months ago” for the first hour or so!

    Thanks for the plugin.

  24. ryan masuga Says:

    -1 years, 12 months ago problem solved by correcting the “Times in the weblog should differ by” form field on the under General Options in the WP admin.

  25. 78.RES Says:

    hi.

    just tried the different time_since plugins all along the web and yours is the only one working for me.
    its such an brilliant idea of user-friendly displaying of time (etc) that i just “translated” the code into german (talkin’ about time and number and the thing with singular/plura is a bit more complicated in german, but nothing what 2 ifs couldn’t solve).

    thanks for the plugin!

    regards,
    max

    (mail me if you want to have the result - or have a watch at blog.REScrew.de -> it is under construction, but time_since should work)

  26. Antonio Says:

    @78.RES:
    hi max, I would be interested in your german mod of the plugin.
    Could you please send it to [toni] at [volloeko|de].

    Thanks in advance!

  27. lening Says:

    I like what you did with the design of your blog! Compliments. Regards, Albert.

  28. TechZ Says:

    Hi, my “Time Offset” is set to +3, because my host time is GMT and I live in Bahrain, +3, so I get the “-1year 12 months” issue. :( Any ideas on how to solve this?

  29. amellie Says:

    with the “-1 year 12 months” issue… I tried to fix it by adding:

    if ($since post_date);

    so far, it works for me :)

  30. amellie Says:

    opps.. sorry.. it seems the code dissapear..

    try adding this
    if ($since '.$original.'‘

    after this line:
    $since = $today - strtotime($post->post_date);

  31. JR Beltman Says:

    I have only been able to test this in the +GMT time zone, so I dont know if it works in the -GMT, but guess it does.

    The first bit of code should be pasted right after where the array is declared.


    $query = "SELECT option_value FROM wp_options WHERE option_name='gmt_offset'";
    $result = mysql_query($query);

    $gmt_offset_wp = mysql_result($result,0)*60*60;
    $gmt_delta_today = tzdelta();
    $today = time(); /* Current unix time */

    $since = $today + $gmt_offset_wp - $gmt_delta_today - strtotime($post->post_date);

    The second part should be pasted just before the ‘?>’ at the end of the plugin code


    function tzdelta ( $iTime = 0 )
    {
    if ( 0 == $iTime ) { $iTime = time(); }
    $ar = localtime ( $iTime );
    $ar[5] += 1900; $ar[4]++;
    $iTztime = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], $ar[8] );
    return ( $iTztime - $iTime );
    }

    How does it work?
    WP seems to take the GMT time + your GMT offset and puts that as the date and time the entry was posted.

    So, we need to get the
    - GMT offset fromthe wp database (sql query in code)
    - GMT time difference of where your server is (mine is in the US somewhere, and I am currently in Sri Lanka)

    Then we use these 2 extra values to correct the value of today(), because that function gives you the local server time.

  32. JR Beltman Says:

    one thing. The quotes / speechmarks or whatever you call these must be replaced with those straight ones that are accepted by PhP. Sorry, but I am not sure how you call these things….

    Let me know if this all works for you

  33. izlesene Says:

    Great

  34. digitalon Says:

    This is my Digital Camera And Cell Phones Blog.
    Full reviews of digital cameras and cellphones reviews… enjoy

  35. outpersonalscom Says:

    outpersonals.com

  36. Manele Says:

    i just installed this plugin on my blog and i love it! thank you!

  37. Elessehaf Says:

    Kawa banga!
    http://index.klassper.cn
    MoNo
    So so

  38. hactuer Says:

    has anyone seen the new game copy pro software package they just came out with.
    looks like it can copy ps2 games. has anyone used it before?

  39. df Says:

    klasfjkljasflkja skgj asdgas

Leave a Reply