<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.dateinterval.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'dateinterval.construct.php',
    1 => 'DateInterval::__construct',
    2 => 'Creates a new DateInterval object',
  ),
  'up' => 
  array (
    0 => 'class.dateinterval.php',
    1 => 'DateInterval',
  ),
  'prev' => 
  array (
    0 => 'class.dateinterval.php',
    1 => 'DateInterval',
  ),
  'next' => 
  array (
    0 => 'dateinterval.createfromdatestring.php',
    1 => 'DateInterval::createFromDateString',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/datetime/dateinterval/construct.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="dateinterval.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateInterval::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateInterval::__construct</span> &mdash; <span class="dc-title">Creates a new DateInterval object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateinterval.construct-description">
  <h3 class="title">Description</h3>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateInterval::__construct</strong></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>

  <p class="para rdfs-comment">
   Creates a new DateInterval object.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateinterval.construct-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">duration</code></dt>
     <dd>
      <p class="para">
       An interval specification.
      </p>
      <p class="para">
       The format starts with the letter <code class="literal">P</code>,
       for <q class="quote">period.</q>
       Each duration period is represented by an integer value
       followed by a period designator.
       If the duration contains time elements, that portion
       of the specification is preceded by the letter
       <code class="literal">T</code>.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>
         <code class="parameter">duration</code> Period Designators
        </strong></caption>
        
         <thead>
          <tr>
           <th>Period Designator</th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><code class="literal">Y</code></td>
           <td>years</td>
          </tr>

          <tr>
           <td><code class="literal">M</code></td>
           <td>months</td>
          </tr>

          <tr>
           <td><code class="literal">D</code></td>
           <td>days</td>
          </tr>

          <tr>
           <td><code class="literal">W</code></td>
           <td>
            weeks. Converted into days.
            Prior to PHP 8.0.0, can not be combined with <code class="literal">D</code>.
           </td>
          </tr>

          <tr>
           <td><code class="literal">H</code></td>
           <td>hours</td>
          </tr>

          <tr>
           <td><code class="literal">M</code></td>
           <td>minutes</td>
          </tr>

          <tr>
           <td><code class="literal">S</code></td>
           <td>seconds</td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       Here are some simple examples.
       Two days is <code class="literal">P2D</code>.
       Two seconds is <code class="literal">PT2S</code>.
       Six years and five minutes is <code class="literal">P6YT5M</code>.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        The unit types must be entered from the largest
        scale unit on the left to the smallest scale unit
        on the right.
        So years before months, months before days,
        days before minutes, etc.
        Thus one year and four days must be represented as
        <code class="literal">P1Y4D</code>, not <code class="literal">P4D1Y</code>.
       </p>
      </p></blockquote>
      <p class="para">
       The specification can also be represented as a date time.
       A sample of one year and four days would be
       <code class="literal">P0001-00-04T00:00:00</code>.
       But the values in this format can not exceed a given period&#039;s
       roll-over-point (e.g. <code class="literal">25</code> hours is invalid).
      </p>
      <p class="para">
       These formats are based on the <a href="http://en.wikipedia.org/wiki/Iso8601#Durations" class="link external">&raquo;&nbsp;ISO 8601 duration
       specification</a>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-dateinterval.construct-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   Throws an <span class="classname"><a href="class.datemalformedintervalstringexception.php" class="classname">DateMalformedIntervalStringException</a></span> when
   the <code class="parameter">duration</code> cannot be parsed as an interval. Prior
   to PHP 8.3, this was <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-dateinterval.construct-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       Now throws
       <span class="exceptionname"><a href="class.datemalformedintervalstringexception.php" class="exceptionname">DateMalformedIntervalStringException</a></span>
       instead of <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>.
      </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,
       including a new <code class="literal">from_string</code> boolean property.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="literal">W</code> can be combined with <code class="literal">D</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-dateinterval.construct-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Constructing and using <span class="classname"><a href="class.dateinterval.php" class="classname">DateInterval</a></span> objects</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Create a specific date<br /></span><span style="color: #0000BB">$someDate </span><span style="color: #007700">= </span><span style="color: #0000BB">\DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d H:i"</span><span style="color: #007700">, </span><span style="color: #DD0000">"2022-08-25 14:18"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Create interval<br /></span><span style="color: #0000BB">$interval </span><span style="color: #007700">= new </span><span style="color: #0000BB">\DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">"P7D"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Add interval<br /></span><span style="color: #0000BB">$someDate</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$interval</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Convert interval to string<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"%d"</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">7</pre>
</div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="classname"><a href="class.dateinterval.php" class="classname">DateInterval</a></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$interval </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1W2D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$interval</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents"><p>Output of the above example in PHP 8.2:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">object(DateInterval)#1 (10) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(9)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;from_string&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
    <div class="example-contents"><p>Output of the above example in PHP 8:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">object(DateInterval)#1 (16) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(9)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;weekday&quot;]=&gt;
  int(0)
  [&quot;weekday_behavior&quot;]=&gt;
  int(0)
  [&quot;first_last_day_of&quot;]=&gt;
  int(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;special_type&quot;]=&gt;
  int(0)
  [&quot;special_amount&quot;]=&gt;
  int(0)
  [&quot;have_weekday_relative&quot;]=&gt;
  int(0)
  [&quot;have_special_relative&quot;]=&gt;
  int(0)
}</pre>
</div>
    </div>
    <div class="example-contents"><p>Output of the above example in PHP 7:</p></div>
    <div class="example-contents screen">
     <div class="annotation-interactive examplescode"><pre class="examplescode">object(DateInterval)#1 (16) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(2)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;weekday&quot;]=&gt;
  int(0)
  [&quot;weekday_behavior&quot;]=&gt;
  int(0)
  [&quot;first_last_day_of&quot;]=&gt;
  int(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;special_type&quot;]=&gt;
  int(0)
  [&quot;special_amount&quot;]=&gt;
  int(0)
  [&quot;have_weekday_relative&quot;]=&gt;
  int(0)
  [&quot;have_special_relative&quot;]=&gt;
  int(0)
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-dateinterval.construct-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="dateinterval.format.php" class="function" rel="rdfs-seeAlso">DateInterval::format()</a> - Formats the interval</span></li>
    <li><span class="function"><a href="datetime.add.php" class="function" rel="rdfs-seeAlso">DateTime::add()</a> - Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds</span></li>
    <li><span class="function"><a href="datetime.sub.php" 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>
    <li><span class="function"><a href="datetime.diff.php" class="function" rel="rdfs-seeAlso">DateTime::diff()</a> - Returns the difference between two DateTime objects</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>