The script below works well enough for my liking, but I was wondering if there was an easier way to go about this. I have a saved time which I am subtracting from time() to see how long ago (in seconds) the event happened. From that, I tried to calculate into minutes, then into hours, then into days. I was also wondering if there was a more percise way of going about this.

This is setup to calculate minutes UNTIL it's greater than 60, then automatically calculate hours. How can I make it calculate hours and minutes at the same time? And how could I get it so I could see how many days and hours it was? (I don't mean if it was 27 hours ago that the script would say 1 day(s) 27 hour(s), I'd want it to say 1 day(s) 3 hour(s)).

my $time=time; my $elapsed = $time - $oldtime; if ($elapsed < "60") { print "<td><center>< 1 min</center></td></tr>\n"; } elsif ($elapsed > "60") { my $newelapsed = $elapsed / 60; my $elapsed2 = substr( $newelapsed, 0, 2 ); print "<td><center>$elapsed2 min(s)</center></td></tr>\n"; } elsif ($elapsed > "3600") { my $newelapsed = $elapsed / (60*60); my $elapsed2 = substr( $newelapsed, 0, 2 ); print "<td><center>$elapsed2 hour(s)</center></td></tr>\n"; } elsif ($elapsed > "43200") { my $newelapsed = $elapsed / (60*60); my $elapsed2 = substr( $newelapsed, 0, 2 ); print "<td><center>$elapsed2 day(s)</center></td></tr>\n"; } }

Any suggestions would be very grateful.


In reply to Determining time since by Anonymous Monk

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.