<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Returns the difference between two DateTime objects</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.datetimeinterface.html">DateTimeInterface</a></div> <div class="next" style="text-align: right; float: right;"><a href="datetime.format.html">DateTime::format</a></div> <div class="up"><a href="class.datetimeinterface.html">DateTimeInterface</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="datetime.diff" class="refentry"> <div class="refnamediv"> <h1 class="refname">DateTime::diff</h1> <h1 class="refname">DateTimeImmutable::diff</h1> <h1 class="refname">DateTimeInterface::diff</h1> <h1 class="refname">date_diff</h1> <p class="verinfo">(PHP 5 >= 5.3.0)</p><p class="refpurpose"><span class="refname">DateTime::diff</span> -- <span class="refname">DateTimeImmutable::diff</span> -- <span class="refname">DateTimeInterface::diff</span> -- <span class="refname">date_diff</span> — <span class="dc-title">Returns the difference between two DateTime objects</span></p> </div> <div class="refsect1 description" id="refsect1-datetime.diff-description"> <h3 class="title">Description</h3> <p class="para">Object oriented style</p> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateTime::diff</strong></span> ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateTimeImmutable::diff</strong></span> ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><strong>DateTimeInterface::diff</strong></span> ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span> ] )</div> <p class="para rdfs-comment">Procedural style</p> <div class="methodsynopsis dc-description"> <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <span class="methodname"><a href="function.date-diff.html" class="methodname">date_diff</a></span> ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime1</code></span> , <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span> ] )</div> <p class="para rdfs-comment"> Returns the difference between two <a href="class.datetimeinterface.html" class="classname">DateTimeInterface</a> objects. </p> </div> <div class="refsect1 parameters" id="refsect1-datetime.diff-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">datetime</code></em></span> <dd> <p class="para"> The date to compare to. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">absolute</code></em></span> <dd> <p class="para"> Should the interval be forced to be positive? </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-datetime.diff-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The <a href="class.dateinterval.html" class="classname">DateInterval</a> object representing the difference between the two dates or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-datetime.diff-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-2273"> <p><strong>Example #1 <span class="function"><strong>DateTime::diff()</strong></span> example</strong></p> <div class="example-contents"><p>Object oriented style</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$datetime1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-11'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$datetime2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-13'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval </span><span style="color: #007700">= </span><span style="color: #0000BB">$datetime1</span><span style="color: #007700">-></span><span style="color: #0000BB">diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$datetime2</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$interval</span><span style="color: #007700">-></span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%R%a days'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>Procedural style</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$datetime1 </span><span style="color: #007700">= </span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-11'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$datetime2 </span><span style="color: #007700">= </span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2009-10-13'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$interval </span><span style="color: #007700">= </span><span style="color: #0000BB">date_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$datetime1</span><span style="color: #007700">, </span><span style="color: #0000BB">$datetime2</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$interval</span><span style="color: #007700">-></span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%R%a days'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above examples will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> +2 days </pre></div> </div> </div> <div class="example" id="example-2274"> <p><strong>Example #2 <a href="class.datetime.html" class="classname">DateTime</a> object comparison</strong></p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> As of PHP 5.2.2, DateTime objects can be compared using <a href="language.operators.comparison.html" class="link">comparison operators</a>. </p> </p></blockquote> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$date1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"now"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$date2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"tomorrow"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1 </span><span style="color: #007700">== </span><span style="color: #0000BB">$date2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1 </span><span style="color: #007700">< </span><span style="color: #0000BB">$date2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1 </span><span style="color: #007700">> </span><span style="color: #0000BB">$date2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> bool(false) bool(true) bool(false) </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-datetime.diff-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="function"><a href="dateinterval.format.html" class="function" rel="rdfs-seeAlso">DateInterval::format()</a> - Formats the interval</span></li> <li class="member"> <span class="function"><a href="datetime.add.html" class="function" rel="rdfs-seeAlso">DateTime::add()</a> - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object</span></li> <li class="member"> <span class="function"><a href="datetime.sub.html" class="function" rel="rdfs-seeAlso">DateTime::sub()</a> - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.datetimeinterface.html">DateTimeInterface</a></div> <div class="next" style="text-align: right; float: right;"><a href="datetime.format.html">DateTime::format</a></div> <div class="up"><a href="class.datetimeinterface.html">DateTimeInterface</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>