Here is a DateTime version, with durations relative to today (accounting for tz/dst)
#!/usr/bin/perl -- use strict; use warnings; use DateTime::Duration; Main( 4, 250, 1.5e4, 4e5, 3e6, 12e6, 1.5e8 ); Main( 4, 250, 1.5e4, 4e5, 3e6 - 4e5, 12e6, 1.5e8 ); exit(0); sub Main { for my $sec (@_) { printf "%9d seconds is about %s\n", $sec, ToPeriod( seconds => $se +c ); } } ## end sub Main sub ToPeriod { my $dur = ( DateTime->now - DateTime->now->subtract(@_) ); for my $unit (qw[ years months weeks days hours minutes seconds nano +seconds ]) { my $nUnit = $dur->in_units($unit); return "$nUnit $unit" if $nUnit; } } ## end sub ToPeriod __END__ 4 seconds is about 4 seconds 250 seconds is about 4 minutes 15000 seconds is about 4 hours 400000 seconds is about 4 days 3000000 seconds is about 1 months 12000000 seconds is about 4 months 150000000 seconds is about 4 years 4 seconds is about 4 seconds 250 seconds is about 4 minutes 15000 seconds is about 4 hours 400000 seconds is about 4 days 2600000 seconds is about 4 weeks 12000000 seconds is about 4 months 150000000 seconds is about 4 years
Alt with DateTime::Format::Human::Duration
#!/usr/bin/perl -- use strict; use warnings; use DateTime::Duration; use DateTime::Format::Human::Duration; Main( 4, 250, 1.5e4, 4e5, 3e6, 12e6, 1.5e8 ); Main( 4, 250, 1.5e4, 4e5, 3e6 - 4e5, 12e6, 1.5e8 ); exit(0); sub Main { for my $sec (@_) { printf "%9d seconds is %s\n", $sec, ToPeriod( seconds => $sec ); } } ## end sub Main sub ToPeriod { return DateTime::Format::Human::Duration->new->format_duration( DateTime->now - DateTime->now->subtract(@_), ); } ## end sub ToPeriod __END__ 4 seconds is 4 seconds 250 seconds is 4 minutes and 10 seconds 15000 seconds is 4 hours and 10 minutes 400000 seconds is 4 days, 15 hours, 6 minutes, and 40 seconds 3000000 seconds is 1 month, 3 days, 17 hours, and 20 minutes 12000000 seconds is 4 months, 2 weeks, 1 day, 21 hours, and 20 minute +s 150000000 seconds is 4 years, 9 months, 1 day, 2 hours, 39 minutes, an +d 58 seconds 4 seconds is 4 seconds 250 seconds is 4 minutes and 10 seconds 15000 seconds is 4 hours and 10 minutes 400000 seconds is 4 days, 15 hours, 6 minutes, and 40 seconds 2600000 seconds is 4 weeks, 2 days, 2 hours, 13 minutes, and 20 seco +nds 12000000 seconds is 4 months, 2 weeks, 1 day, 21 hours, and 20 minute +s 150000000 seconds is 4 years, 9 months, 1 day, 2 hours, 39 minutes, an +d 58 seconds
DateTime::Format::Duration also looked promising (more customizable, uses strftime like specifiers), but weeks calculation breaks months/years.

In reply to Re: Approximate time by Anonymous Monk
in thread Approximate time by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.