<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'dateinterval.construct.php',
    1 => 'DateInterval::__construct',
    2 => 'Erstellt ein neues DateInterval-Objekt',
  ),
  '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' => 'de',
    '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">Erstellt ein neues DateInterval-Objekt</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateinterval.construct-description">
  <h3 class="title">Beschreibung</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">
   Erstellt ein neues DateInterval-Objekt.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateinterval.construct-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">duration</code></dt>
     <dd>
      <p class="para">
       Eine Intervall-Spezifikation
      </p>
      <p class="para">
       Das Format beginnt mit dem Buchstaben <code class="literal">P</code> für
       <q class="quote">period</q>. Jede Periodendauer wird als Integer-Wert,
       gefolgt von einem Perioden-Bezeichner, repräsentiert. Enthält die
       Dauer Zeitelemente, wird diesem Teil der Spezifikation der Buchstabe
       <code class="literal">T</code> vorangestellt.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>
         Perioden-Bezeichner für <code class="parameter">duration</code>
        </strong></caption>
        
         <thead>
          <tr>
           <th>Perioden-Bezeichner</th>
           <th>Beschreibung</th>
          </tr>

         </thead>

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

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

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

          <tr>
           <td><code class="literal">W</code></td>
           <td>
            Wochen. Diese werden in Tage umgerechnet.
            Vor PHP 8.0.0 konnte es nicht mit <code class="literal">D</code> kombiniert werden.
           </td>
          </tr>

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

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

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

         </tbody>
        
       </table>

      </p>
      <p class="para">
       Hier sind ein paar einfache Beispiele:
       Zwei Tage entsprechen <code class="literal">P2D</code>.
       Zwei Sekunden entsprechen <code class="literal">PT2S</code>.
       Sechs Jahre und fünf Minuten entsprechen <code class="literal">P6YT5M</code>.
       </p>
      <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
       <p class="para">
        Die Einheitstypen müssen von der größten Einheit links bis zur
        kleinsten Einheit rechts angegeben werden. Also Jahre vor Monaten,
        Monate vor Tagen, Tage vor Minuten etc. Also muss ein Jahr und vier
        Tage als <code class="literal">P1Y4D</code> dargestellt werden, nicht als
        <code class="literal">P4D1Y</code>.
       </p>
      </p></blockquote>
      <p class="para">
       Die Spezifikation kann aber auch als Zeitstempel angegeben werden.
       Ein Beispiel für ein Jahr und vier Tage wäre also
       <code class="literal">P0001-00-04T00:00:00</code>.
       Die Werte in diesem Format können allerdings den jeweiligen
       Übersprungspunkt der Periode nicht überschreiten
       (z. B. ist <code class="literal">25</code> Stunden ungültig).
      </p>
      <p class="para">
       Diese Formate basieren auf der
       <a href="http://en.wikipedia.org/wiki/Iso8601#Durations" class="link external">&raquo;&nbsp;ISO 8601-Spezifikation für die Dauer</a>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-dateinterval.construct-errors">
  <h3 class="title">Fehler/Exceptions</h3>
  <p class="para">
   Wenn <code class="parameter">duration</code> nicht als Intervall geparst werden
   kann, wird eine <span class="classname"><a href="class.datemalformedintervalstringexception.php" class="classname">DateMalformedIntervalStringException</a></span>
   geworfen. Vor PHP 8.3 wird eine <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>
   geworfen.
  </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>Beschreibung</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       Wirft nun eine
       <span class="exceptionname"><a href="class.datemalformedintervalstringexception.php" class="exceptionname">DateMalformedIntervalStringException</a></span>
       anstelle von <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>.
      </td>
     </tr>

     <tr>
      <td>8.2.0</td>
      <td>
       Nur die Eigenschaften <code class="literal">y</code> bis <code class="literal">f</code>,
       <code class="literal">invert</code> und <code class="literal">days</code> sowie die neue
       boolesche Eigenschaft <code class="literal">from_string</code> sind sichtbar.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="literal">W</code> kann mit <code class="literal">D</code> kombiniert werden.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-dateinterval.construct-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Erstellen und Verwenden von <span class="classname"><a href="class.dateinterval.php" class="classname">DateInterval</a></span>-Objekten</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">// Ein konkretes Datum erstellen<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">// Ein Intervall erstellen<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">// Das Intervall hinzufügen<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">// Das Intervall in eine Zeichenkette umwandeln<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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</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>Beispiel #2 <span class="classname"><a href="class.dateinterval.php" class="classname">DateInterval</a></span>-Beispiel</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>Das oben gezeigte Beispiel erzeugt mit PHP 8.2 folgende Ausgabe:</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>Das oben gezeigte Beispiel erzeugt mit PHP 8 folgende Ausgabe:</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>Das oben gezeigte Beispiel erzeugt mit PHP 7 folgende Ausgabe:</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">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="dateinterval.format.php" class="function" rel="rdfs-seeAlso">DateInterval::format()</a> - Formatiert ein Intervall</span></li>
    <li><span class="function"><a href="datetime.add.php" class="function" rel="rdfs-seeAlso">DateTime::add()</a> - &Auml;ndert ein DateTime-Objekt, wobei die Anzahl der Tage, Monate, Jahre,
   Stunden, Minuten und Sekunden hinzugef&uuml;gt wird</span></li>
    <li><span class="function"><a href="datetime.sub.php" class="function" rel="rdfs-seeAlso">DateTime::sub()</a> - Subtrahiert eine Anzahl von Tagen, Monaten, Jahren, Stunden, Minuten und
   Sekunden von einem DateTime-Objekt.</span></li>
    <li><span class="function"><a href="datetime.diff.php" class="function" rel="rdfs-seeAlso">DateTime::diff()</a> - Liefert die Differenz zwischen zwei DateTime-Objekten</span></li>
   </ul>
  </p>
 </div>


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