Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Borrowing most of Guffa's answer, this should do the trick as a jQuery plugin:</p> <pre><code>jQuery.fn.time_from_seconds = function() { return this.each(function() { var t = parseInt($(this).text(), 10); $(this).data('original', t); var h = Math.floor(t / 3600); t %= 3600; var m = Math.floor(t / 60); var s = Math.floor(t % 60); $(this).text((h &gt; 0 ? h + ' hour' + ((h &gt; 1) ? 's ' : ' ') : '') + (m &gt; 0 ? m + ' minute' + ((m &gt; 1) ? 's ' : ' ') : '') + s + ' second' + ((s &gt; 1) ? 's' : '')); }); }; </code></pre> <p>If you have HTML like this:</p> <pre><code>&lt;span class='time'&gt;67&lt;/span&gt; &lt;span class='time'&gt;953&lt;/span&gt; &lt;span class='time'&gt;3869&lt;/span&gt; </code></pre> <p>And you call it like this:</p> <pre><code>$('.time').time_from_seconds(); </code></pre> <p>The HTML is turned to:</p> <pre><code>&lt;span class="time"&gt;1 minute 7 seconds&lt;/span&gt; &lt;span class="time"&gt;15 minutes 53 seconds&lt;/span&gt; &lt;span class="time"&gt;1 hour 4 minutes 29 seconds&lt;/span&gt; </code></pre> <p>Each element also has a <a href="http://docs.jquery.com/Data" rel="noreferrer">data attribute</a> of 'original' with the seconds it originally contained.</p> <p>My answer directly answers your question, but I'm going to take a shot in the dark: if you want to show how long ago something happened in human time (ie, "5 minutes ago") there is the <a href="http://timeago.yarp.com/" rel="noreferrer">jQuery timeago plugin</a> for this. I don't think it accepts seconds as the format, though. It has to be a <a href="http://en.wikipedia.org/wiki/ISO_8601" rel="noreferrer">ISO 8601</a> date.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload