Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://php.net/manual/en/ref.datetime.php" rel="nofollow">http://php.net/manual/en/ref.datetime.php</a></p> <p>Just change $precision to 1 when you call the function and add in whatever text you want to come before and after the date. You'll have to make sure you convert your date objects to timestamps, but that shouldn't be a problem for you. <pre><code>/** * this code assumes php &gt;= 5.1.0. if using &lt; 5.1, read * php.net/strtotime and change the condition for checking * for failure from strtotime() */ // $t1, $t2: unix times, or strtotime parseable // $precision: max number of units to output // $abbr: if true, use "hr" instead of "hour", etc. function date_diff ($t1, $t2, $precision = 6, $abbr = false) { if (preg_match('/\D/', $t1) &amp;&amp; ($t1 = strtotime($t1)) === false) return false; if (preg_match('/\D/', $t2) &amp;&amp; ($t2 = strtotime($t2)) === false) return false; if ($t1 &gt; $t2) list($t1, $t2) = array($t2, $t1); $diffs = array( 'year' =&gt; 0, 'month' =&gt; 0, 'day' =&gt; 0, 'hour' =&gt; 0, 'minute' =&gt; 0, 'second' =&gt; 0, ); $abbrs = array( 'year' =&gt; 'yr', 'month' =&gt; 'mth', 'day' =&gt; 'day', 'hour' =&gt; 'hr', 'minute' =&gt; 'min', 'second' =&gt; 'sec' ); foreach (array_keys($diffs) as $interval) { while ($t2 &gt;= ($t3 = strtotime("+1 ${interval}", $t1))) { $t1 = $t3; ++$diffs[$interval]; } } $stack = array(); foreach ($diffs as $interval =&gt; $num) $stack[] = array($num, ($abbr ? $abbrs[$interval] : $interval) . ($num != 1 ? 's' : '')); $ret = array(); while (count($ret) &lt; $precision &amp;&amp; ($item = array_shift($stack)) !== null) { if ($item[0] &gt; 0) $ret[] = "{$item[0]} {$item[1]}"; } return implode(', ', $ret); } $t1 = 'Feb 4, 2008 12:16:00'; $t2 = 'Jul 3, 2006 16:15:30'; echo date_diff($t1, $t2), "\n", date_diff($t1, $t2, 3), "\n", date_diff($t1, $t2, 2, true), "\n"; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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