Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best course of action is using PHP's <strong><a href="http://il.php.net/manual/en/class.datetime.php" rel="noreferrer"><code>DateTime</code></a></strong> (and <strong><a href="http://il.php.net/manual/en/class.dateinterval.php" rel="noreferrer"><code>DateInterval</code></a></strong>) objects. Each date is encapsulated in a <code>DateTime</code> object, and then a difference between the two can be made:</p> <pre><code>$first_date = new DateTime("2012-11-30 17:03:30"); $second_date = new DateTime("2012-12-21 00:00:00"); </code></pre> <p>The <code>DateTime</code> object will accept any format <code>strtotime()</code> would. If an even more specific date format is needed, <strong><a href="http://il.php.net/manual/en/datetime.createfromformat.php" rel="noreferrer"><code>DateTime::createFromFormat()</code></a></strong> can be used to create the <code>DateTime</code> object.</p> <p>After both objects were instantiated, you substract one from the other with <strong><a href="http://il.php.net/manual/en/datetime.diff.php" rel="noreferrer"><code>DateTime::diff()</code></a></strong>.</p> <pre><code>$difference = $first_date-&gt;diff($second_date); </code></pre> <p><code>$difference</code> now holds a <a href="http://il.php.net/manual/en/class.dateinterval.php" rel="noreferrer"><code>DateInterval</code></a> object with the difference information. A <code>var_dump()</code> looks like this:</p> <pre><code>object(DateInterval) public 'y' =&gt; int 0 public 'm' =&gt; int 0 public 'd' =&gt; int 20 public 'h' =&gt; int 6 public 'i' =&gt; int 56 public 's' =&gt; int 30 public 'invert' =&gt; int 0 public 'days' =&gt; int 20 </code></pre> <p>To format the <code>DateInterval</code> object, we'll need check each value and exclude it if it's 0:</p> <pre><code>/** * Format an interval to show all existing components. * If the interval doesn't have a time component (years, months, etc) * That component won't be displayed. * * @param DateInterval $interval The interval * * @return string Formatted interval string. */ function format_interval(DateInterval $interval) { $result = ""; if ($interval-&gt;y) { $result .= $interval-&gt;format("%y years "); } if ($interval-&gt;m) { $result .= $interval-&gt;format("%m months "); } if ($interval-&gt;d) { $result .= $interval-&gt;format("%d days "); } if ($interval-&gt;h) { $result .= $interval-&gt;format("%h hours "); } if ($interval-&gt;i) { $result .= $interval-&gt;format("%i minutes "); } if ($interval-&gt;s) { $result .= $interval-&gt;format("%s seconds "); } return $result; } </code></pre> <p>All that's left now is to call our function on the <code>$difference</code> <code>DateInterval</code> object:</p> <pre><code>echo format_interval($difference); </code></pre> <p>And we get the correct result:</p> <blockquote> <p>20 days 6 hours 56 minutes 30 seconds</p> </blockquote> <p>The complete code used to achieve the goal:</p> <pre><code>/** * Format an interval to show all existing components. * If the interval doesn't have a time component (years, months, etc) * That component won't be displayed. * * @param DateInterval $interval The interval * * @return string Formatted interval string. */ function format_interval(DateInterval $interval) { $result = ""; if ($interval-&gt;y) { $result .= $interval-&gt;format("%y years "); } if ($interval-&gt;m) { $result .= $interval-&gt;format("%m months "); } if ($interval-&gt;d) { $result .= $interval-&gt;format("%d days "); } if ($interval-&gt;h) { $result .= $interval-&gt;format("%h hours "); } if ($interval-&gt;i) { $result .= $interval-&gt;format("%i minutes "); } if ($interval-&gt;s) { $result .= $interval-&gt;format("%s seconds "); } return $result; } $first_date = new DateTime("2012-11-30 17:03:30"); $second_date = new DateTime("2012-12-21 00:00:00"); $difference = $first_date-&gt;diff($second_date); echo format_interval($difference); </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