### How do I convert seconds into a readable time?

 on Aug 02, 2001 at 02:36 UTC Need Help??
Contributed by Monolith-0 on Aug 02, 2001 at 02:36 UTC
Q&A  > dates and times

#### Description:

ex. Change 196364 into '2 days, 6 hours, 32 minutes and 44 seconds' or something like that.

 Answer: How do I convert seconds into a readable time?contributed by I0 ```printf "%d days, %d hours, %d minutes and %d seconds\n",(gmtime 196364 +)[7,2,1,0]; [download]``` But NB: this only works for intervals less than one month. tachyon's method (below) works for arbitrarily large values (within integer range; this could be fixed by using Math::BigInt). Answer: How do I convert seconds into a readable time?contributed by tachyon I love IOs answer but in the spirit of TIMTOWDI here is how to roll your own. This illustrates one of the uses for the modulus operator: ```my \$sec = 196364; print "days ", int(\$sec/(24*60*60)), "\n"; print "hours ", (\$sec/(60*60))%24, "\n"; print "mins ", (\$sec/60)%60, "\n"; print "secs ", \$sec%60, "\n"; [download]``` Answer: How do I convert seconds into a readable time?contributed by Limbic~Region You could use Time::Duration. The question was intriguing enough to me to re-invent a wheel for the sake of learning. Cheers - L~R Answer: How do I convert seconds into a readable time?contributed by toolic If you only need an approximate answer, I'll invoke the "or something like that" clause and change 196364 into '2.3d' ```print sec2human(196364), "\n"; sub sec2human { my \$secs = shift; if (\$secs >= 365*24*60*60) { return sprintf '%.1fy', \$secs/(365 +*24*60*60) } elsif (\$secs >= 24*60*60) { return sprintf '%.1fd', \$secs/( + 24*60*60) } elsif (\$secs >= 60*60) { return sprintf '%.1fh', \$secs/( + 60*60) } elsif (\$secs >= 60) { return sprintf '%.1fm', \$secs/( + 60) } else { return sprintf '%.1fs', \$secs + } } [download]``` Answer: How do I convert seconds into a readable time?contributed by zen-japh Here is a method of going back to seconds from a string like "2 days, 6 hours, 32 minutes and 44 seconds": ```sub dhms2sec { my \$in = shift; \$in =~ s/(and|,)//g; \$in =~ s/(\w+)s/\1/g; my %y = reverse split(/\s+/,\$in); return (\$y{'second'}) + (\$y{'minute'} * 60) + (\$y{'hour'} * 60*60) + (\$y{'day'} * 60*60*24); } [download]``` Answer: How do I convert seconds into a readable time?contributed by hossman The DateTime project has created a DateTime::Duration object, as well as DateTime::Format::Duration.

• 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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [Eily]: what? I thought PHP was known for its reliable and consistent behaviour and documentation! [LanX]: ... and a lot of pseudo Perl experts [Eily]: though honestly, I'm sure I have seen people use closure to mean anonysub for perl as well [stonecolddevin]: good morning friends [LanX]: yes, mainly pseudo Perl experts [Eily]: good morning stonecolddevin LanX has to go/ [Eily]: bye LanX [LanX]: bye :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2018-02-23 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (305 votes). Check out past polls.

Notices?