glwtta,
I seriously recommend you use one of the modules already recommended. I was interested in the problem, so I rolled my own purely for learning purposes. I wouldn't recommend you actually use this code (using AUTOLOAD, no POD, etc) - but you might be able to learn from it. I am not sure how far along "the path" you are.
package Seconds2English; use strict; use vars '$AUTOLOAD'; sub new { my ($class, $start_value, %opts) = @_; my $self = bless {}, $class; $self->_build($start_value, \%opts); return $self; } sub _build { my ($self, $start_value, $opt_ref) = @_; $start_value = abs(int($start_value)); return 0 if ($start_value == 0); $self->{'time'} = $start_value; $self->{'mod_hash'} = { 'minute' => 60, 'hour' => 3_600, 'day' => 86_400, 'week' => 604_800, }; if (exists $opt_ref->{'month'} && $opt_ref->{'month'}) { $self->{'mod_hash'}{'month'} = $opt_ref->{'month'}; } else { $self->{'mod_hash'}{'month'} = 2_592_000; } if (exists $opt_ref->{'year'} && $opt_ref->{'year'}) { $self->{'mod_hash'}{'year'} = $opt_ref->{'year'}; } else { $self->{'mod_hash'}{'year'} = 31_536_000; } for my $unit ( qw(year month week day hour minute) ) { if ($start_value % $self->{'mod_hash'}{$unit} != $start_value) + { $self->{$unit} = int($start_value / $self->{'mod_hash'}{$u +nit}); push @{$self->{'units'}}, "$self->{$unit} $unit"; ${$self->{'units'}}[-1] .= 's' if ($self->{$unit} > 1); $start_value = $start_value % $self->{'mod_hash'}{$unit}; } } if ($start_value) { $self->{'second'} = $start_value; push @{$self->{'units'}}, "$self->{'second'} second"; ${$self->{'units'}}[-1] .= 's' if ($start_value > 1); } if (@{$self->{'units'}} > 2) { my $last = pop @{$self->{'units'}}; $self->{'english'} = join ', ', @{$self->{'units'}}, "and $las +t"; } elsif (@{$self->{'units'}} > 1) { $self->{'english'} = "${$self->{'units'}}[0] and ${$self->{'un +its'}}[1]"; } elsif (@{$self->{'units'}} > 0) { $self->{'english'} = "${$self->{'units'}}[0]"; } } sub AUTOLOAD { return if (our $AUTOLOAD =~ /::DESTROY$/); my $self = shift; my ($sub) = $AUTOLOAD =~ /.*::(\w+)$/; if (exists $self->{$sub}) { return $self->{$sub}; } elsif (grep /\b$sub\b/, qw(year month week day hour minute second +english time)) { return 0; } else { die "\n$sub is not valid and the author has done a poor job of + documentation\n"; } } 1; ################################################# # # And then the testscript.pl to see how it works # ################################################# #!/usr/bin/perl -w use strict; use Seconds2English; my $object = Seconds2English->new(1000000); print $object->year(), "\n"; print $object->month(), "\n"; print $object->week(), "\n"; print $object->day(), "\n"; print $object->hour(), "\n"; print $object->minute(), "\n"; print $object->second(), "\n"; print "This is what you were after\n"; print $object->english(), "\n"; print "The original time entered was\n"; print $object->time(), "\n";
Learning POD is on my "to-do" list, so I did a bad job of documenting the methods by using each one of them once in the test script. You can overide the number of seconds in a month or year. I used 30 days for month and 365 days for a year.
  • my $object = Seconds2English->new(1000000, 'month' => 2_678_400);

    Cheers - L~R

    Update: If you are interested, I will update this later with POD and give better examples as well as make sure that it works with all kinds of test data - just let me know.


    In reply to Re: "natural language" time formatting? by Limbic~Region
    in thread "natural language" time formatting? by glwtta

    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.