<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.datetime.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'class.dateinterval.php',
    1 => 'DateInterval',
    2 => 'The DateInterval class',
  ),
  'up' => 
  array (
    0 => 'book.datetime.php',
    1 => 'Date/Time',
  ),
  'prev' => 
  array (
    0 => 'datetimezone.listidentifiers.php',
    1 => 'DateTimeZone::listIdentifiers',
  ),
  'next' => 
  array (
    0 => 'dateinterval.construct.php',
    1 => 'DateInterval::__construct',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/datetime/dateinterval.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.dateinterval.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.dateinterval" class="reference">

 <h1 class="title">The DateInterval class</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p>


  <div class="section" id="dateinterval.intro">
   <h2 class="title">Introduction</h2>
   <p class="para">
    Represents a date interval.
   </p>
   <p class="para">
    A date interval stores either a fixed amount of time (in years, months,
    days, hours etc) or a relative time string in the format that
    <span class="classname"><a href="class.datetimeimmutable.php" class="classname">DateTimeImmutable</a></span>&#039;s and
    <span class="classname"><a href="class.datetime.php" class="classname">DateTime</a></span>&#039;s constructors support.
   </p>
   <p class="para">
    More specifically, the information in an object of the
    <span class="classname"><strong class="classname">DateInterval</strong></span> class is an instruction to get from
    one date/time to another date/time. This process is not always reversible.
   </p>
   <p class="para">
    A common way to create a <span class="classname"><strong class="classname">DateInterval</strong></span> object
    is by calculating the difference between two date/time objects through
    <span class="methodname"><a href="datetime.diff.php" class="methodname">DateTimeInterface::diff()</a></span>.
   </p>
   <p class="para">
    Since there is no well defined way to compare date intervals,
    <span class="classname"><strong class="classname">DateInterval</strong></span> instances are
    <a href="language.operators.comparison.php#language.operators.comparison.incomparable" class="link">incomparable</a>.
   </p>
  </div>


  <div class="section" id="dateinterval.synopsis">
   <h2 class="title">Class synopsis</h2>


   <div class="classsynopsis"><div class="classsynopsisinfo">
    
     <span class="modifier">class</span> <strong class="classname"><strong class="classname">DateInterval</strong></strong>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Properties */</div>
    <div class="fieldsynopsis">
     <span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.y">$<var class="varname">y</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.m">$<var class="varname">m</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.d">$<var class="varname">d</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.h">$<var class="varname">h</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.i">$<var class="varname">i</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.s">$<var class="varname">s</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.float.php" class="type float">float</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.f">$<var class="varname">f</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.invert">$<var class="varname">invert</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.days">$<var class="varname">days</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.from-string">$<var class="varname">from_string</var></a></var>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
      <var class="varname"><a href="class.dateinterval.php#dateinterval.props.date-string">$<var class="varname">date_string</var></a></var>;</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="dateinterval.construct.php" class="methodname">__construct</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$duration</code></span>)</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><a href="dateinterval.createfromdatestring.php" class="methodname">createFromDateString</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$datetime</code></span>): <span class="type"><a href="class.dateinterval.php" class="type DateInterval">DateInterval</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="dateinterval.format.php" class="methodname">format</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$format</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

   }</div>


  </div>
  
  <div class="section" id="dateinterval.props">
   <h2 class="title">Properties</h2>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="para">
     The available properties listed below depend on PHP version, and should
     be considered as <em>readonly</em>.
    </p>
   </div>
   <dl>
    
     <dt id="dateinterval.props.y"><var class="varname">y</var></dt>
     <dd>
      <p class="para">
       Number of years.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.m"><var class="varname">m</var></dt>
     <dd>
      <p class="para">
       Number of months.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.d"><var class="varname">d</var></dt>
     <dd>
      <p class="para">
       Number of days.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.h"><var class="varname">h</var></dt>
     <dd>
      <p class="para">
       Number of hours.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.i"><var class="varname">i</var></dt>
     <dd>
      <p class="para">
       Number of minutes.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.s"><var class="varname">s</var></dt>
     <dd>
      <p class="para">
       Number of seconds.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.f"><var class="varname">f</var></dt>
     <dd>
      <p class="para">
       Number of microseconds, as a fraction of a second.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.invert"><var class="varname">invert</var></dt>
     <dd>
      <p class="para">
       Is <code class="literal">1</code> if the interval
       represents a negative time period and
       <code class="literal">0</code> otherwise.
       See <span class="methodname"><a href="dateinterval.format.php" class="methodname">DateInterval::format()</a></span>.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.days"><var class="varname">days</var></dt>
     <dd>
      <p class="para">
       If the DateInterval object was created by
       <span class="methodname"><a href="datetime.diff.php" class="methodname">DateTimeImmutable::diff()</a></span> or
       <span class="methodname"><a href="datetime.diff.php" class="methodname">DateTime::diff()</a></span>, then this is the
       total number of full days between the start and end dates. Otherwise,
       <var class="varname">days</var> will be <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.from-string"><var class="varname">from_string</var></dt>
     <dd>
      <p class="para">
       If the DateInterval object was created by
       <span class="methodname"><a href="dateinterval.createfromdatestring.php" class="methodname">DateInterval::createFromDateString()</a></span>, then
       this property&#039;s value will be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, and the
       <var class="varname">date_string</var> property will be populated. Otherwise,
       the value will be <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, and the <var class="varname">y</var> to
       <var class="varname">f</var>, <var class="varname">invert</var>, and
       <var class="varname">days</var> properties will be populated.
      </p>
     </dd>
    
    
     <dt id="dateinterval.props.date-string"><var class="varname">date_string</var></dt>
     <dd>
      <p class="para">
       The string used as argument to
       <span class="methodname"><a href="dateinterval.createfromdatestring.php" class="methodname">DateInterval::createFromDateString()</a></span>.
      </p>
     </dd>
    
   </dl>
  </div>

  <div class="section" id="dateinterval.changelog">
   <h2 class="title">Changelog</h2>
   <p class="para">
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>Version</th>
        <th>Description</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>8.2.0</td>
        <td>
         The <var class="varname">from_string</var> and <var class="varname">date_string</var>
         properties were added for <span class="classname"><strong class="classname">DateInterval</strong></span>
         instances that were created using the
         <span class="methodname"><a href="dateinterval.createfromdatestring.php" class="methodname">DateInterval::createFromDateString()</a></span> method.
        </td>
       </tr>

       <tr>
        <td>8.2.0</td>
        <td>
         Only the <code class="literal">y</code> to <code class="literal">f</code>,
         <code class="literal">invert</code>, and <code class="literal">days</code> will be visible.
        </td>
       </tr>

       <tr>
        <td>7.4.0</td>
        <td>
         <span class="classname"><strong class="classname">DateInterval</strong></span> instances are incomparable now;
         previously, all <span class="classname"><strong class="classname">DateInterval</strong></span> instances were considered equal.
        </td>
       </tr>

       <tr>
        <td>7.1.0</td>
        <td>The <var class="varname">f</var> property was added.</td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </div>

 </div>

 















<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="dateinterval.construct.php">DateInterval::__construct</a> — Creates a new DateInterval object</li><li><a href="dateinterval.createfromdatestring.php">DateInterval::createFromDateString</a> — Sets up a DateInterval from the relative parts of the string</li><li><a href="dateinterval.format.php">DateInterval::format</a> — Formats the interval</li></ul>
</div>
<?php manual_footer($setup); ?>