Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the following function and call it as <code>format_interval(time() - $saved_timestamp)</code>, where <code>$saved_timestamp</code> is the timestamp of the "event" you are interested in. (The following code is licensed under <em>GNU General Public License v2 or successive</em>.)</p> <pre><code>function format_interval($interval, $granularity = 2) { $units = array('1 year|@count years' =&gt; 31536000, '1 week|@count weeks' =&gt; 604800, '1 day|@count days' =&gt; 86400, '1 hour|@count hours' =&gt; 3600, '1 min|@count min' =&gt; 60, '1 sec|@count sec' =&gt; 1); $output = ''; foreach ($units as $key =&gt; $value) { $key = explode('|', $key); if ($interval &gt;= $value) { $floor = floor($interval / $value); $output .= ($output ? ' ' : '') . ($floor == 1 ? $key[0] : str_replace('@count', $floor, $key[1])); $interval %= $value; $granularity--; } if ($granularity == 0) { break; } } return $output ? $output : '0 sec'; } </code></pre> <p>$granularity is the number of different units to show. For example, <code>format_interval(32745600)</code> would return <code>"1 year 2 weeks"</code>.</p> <p>The code I am showing is a reduced version of <a href="https://api.drupal.org/api/drupal/includes%21common.inc/function/format_interval/7.x" rel="nofollow noreferrer"><code>format_interval()</code></a> that comes with Drupal 7, which is code distributed under <a href="https://cgit.drupalcode.org/drupal/tree/LICENSE.txt?h=7.x" rel="nofollow noreferrer">GNU General Public License v2 or successive</a> license. (See also <a href="https://cgit.drupalcode.org/drupal/tree/COPYRIGHT.txt?h=7.x" rel="nofollow noreferrer">COPYRIGHT.txt</a>)<br> I took off the part that is very specific of Drupal, and left the code that uses plain PHP functions.</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