Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Showing the time difference in a natural format?

by skx (Parson)
on Sep 21, 2008 at 21:12 UTC ( #712887=perlquestion: print w/ replies, xml ) Need Help??
skx has asked for the wisdom of the Perl Monks concerning the following question:

Many websites manage to show date differences in a "natural" format. With forum posts, comments, and similar things shown as:

  • "Just now"
  • "10 minutes ago".
  • "2 months ago".

I've written the following, but it is very naive, and I'm surprised there isn't a CPAN module I could find to do the job properly. Is there anything obvious I've missed, or even a better way to handle this type of problem?

#!/usr/bin/perl -w use strict; my $now = time; my $then = $now - ( 60 * 60 * 6 ); print difference( $now - $then ); sub difference { my( $seconds ) = (@_ ); if ( $seconds < 60 ) { # less than a minute return( "Just now" ); } if ( $seconds <= ( 60 * 60 ) ) { # less than an hour return( int($seconds/ 60 ) . " minutes ago" ); } if ( $seconds <= ( 60 * 60 * 24 ) ) { # less than a day return( int( $seconds/(60 * 60) ) . " hours ago" ); } if ( $seconds <= ( 60 * 60 * 24 * 7 ) ) { # less than a week return( int( $seconds/(60*60*24)) . " days ago" ); } # fall-back weeks ago return( int( $seconds/(60*60*24*7)) . " weeks ago" ); }
Steve
--

Comment on Showing the time difference in a natural format?
Download Code
Replies are listed 'Best First'.
Re: Showing the time difference in a natural format? (Time::Duration)
by ikegami (Pope) on Sep 21, 2008 at 21:39 UTC

    I found Time::Duration

    ago( $now - $then, 1 )

    Caveats:

    • It simplifies by using year = 365 days and day = 24 hours.
    • It rounds instead of truncating.

      That looks ideal for me, even with the simplifications. ++

      Looking at the name I'm amazed I didn't find that myself.

      Steve
      --
        I searched for "time ago". Unfortunately, many time zones have "ago" in their name (such as "Chicago"). And it either ignored "ago" or it came up lots of irrelevant Time:: modules after those that had "time" and "ago" in their name.

        Update: Since I couldn't stop after the first sentence, the post lost all value. Ignore it :)

Re: Showing the time difference in a natural format?
by Anonymous Monk on Sep 22, 2008 at 00:47 UTC
    Determining time since
    http://search.cpan.org/search?query=datetime+ago&mode=all
    DateTime::Format::Human::Duration
    print $dt->format_duration_between( $dta, $dtb, 'past' => 'Your account expired %s ago.', 'future' => 'Your account expires in %s.', 'no_time'=> 'Your account just expired.', );
    DateTime::Format::Natural::Lang::EN
    6 in the morning 4 in the afternoon 9 in the evening 25 seconds ago 10 minutes ago 7 hours ago 40 days ago 2 weeks ago 5 months ago 3 years ago tomorrow 25 seconds ago tomorrow 10 minutes ago tomorrow 7 hours ago tomorrow 40 day ago tomorrow 2 weeks ago tomorrow 5 months ago tomorrow 3 years ago yesterday 25 seconds ago yesterday 10 minutes ago yesterday 7 hours ago yesterday 40 days ago yesterday 2 weeks ago yesterday 5 months ago yesterday 3 years ago fri 3 months ago at 5am wednesday 1 month ago at 8pm 8 hours before now 8 hours from now 7 days before now 7 days from now 4 weeks before now 4 weeks from now 13 months before now 13 months from now 2 years before now 2 years from now tuesday 4 in the morning thursday 2 in the afternoon monday 6 in the evening last sunday at 21:45 last week friday monday last week 2nd day last week 10th day last month tuesday next week 3rd day next month 10th month next year in 42 minutes in 3 hours in 5 days wednesday this week 3rd tuesday this november 3 hours before tomorrow 6 hours before yesterday 9 hours after tomorrow 12 hours after yesterday 3 hours before noon 6 hours after noon 9 hours before midnight 12 hours after midnight yesterday at noon yesterday at midnight today at noon today at midnight tomorrow at noon tomorrow at midnight yesterday at 16:00 today at 6:00 tomorrow at 12 wednesday at 14:30 2nd friday in august tomorrow 1 year ago saturday 3 months ago at 5:00pm 4th day last week 3rd wednesday in november 3rd month next year

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://712887]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2015-07-29 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls