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

contributors($setup);

?>
<div id="datetimeimmutable.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTimeImmutable::__construct</h1>
  <h1 class="refname">date_create_immutable</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTimeImmutable::__construct</span> -- <span class="refname">date_create_immutable</span> &mdash; <span class="dc-title">Returns new DateTimeImmutable object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetimeimmutable.construct-description">
  <h3 class="title">Descrizione</h3>
  <p class="para">Stile orientato agli oggetti</p>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateTimeImmutable::__construct</strong></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 class="initializer"> = &quot;now&quot;</span></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="class.datetimezone.php" class="type DateTimeZone">DateTimeZone</a></span></span> <code class="parameter">$timezone</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>)</div>

  <p class="para rdfs-comment">Stile procedurale</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><a href="function.date-create-immutable.php" class="methodname">date_create_immutable</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 class="initializer"> = &quot;now&quot;</span></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="class.datetimezone.php" class="type DateTimeZone">DateTimeZone</a></span></span> <code class="parameter">$timezone</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><span class="type"><a href="class.datetimeimmutable.php" class="type DateTimeImmutable">DateTimeImmutable</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

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


 <div class="refsect1 parameters" id="refsect1-datetimeimmutable.construct-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <dl>
   
    <dt><code class="parameter">datetime</code></dt>
    <dd>
     <p class="para">Una stringa data/ora. I formati validi sono descritti in <a href="datetime.formats.php" class="link">Formati di Data e Tempo</a>.</p>
     <p class="para">
      Enter <code class="literal">&quot;now&quot;</code> here to obtain the current time when using
      the <code class="parameter">$timezone</code> parameter.
     </p>
    </dd>
   
   
    <dt><code class="parameter">timezone</code></dt>
    <dd>
     <p class="para">
      A <span class="classname"><a href="class.datetimezone.php" class="classname">DateTimeZone</a></span> object representing the
      timezone of <code class="parameter">$datetime</code>.
     </p>
     <p class="para">
      If <code class="parameter">$timezone</code> is omitted or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>,
      the current timezone will be used.
     </p>
     <blockquote class="note"><p><strong class="note">Nota</strong>: 
      <p class="para">
       The <code class="parameter">$timezone</code> parameter
       and the current timezone are ignored when the
       <code class="parameter">$datetime</code> parameter either
       is a UNIX timestamp (e.g. <code class="literal">@946684800</code>)
       or specifies a timezone
       (e.g. <code class="literal">2010-01-28T15:00:00+02:00</code>, or
       <code class="literal">2010-07-05T06:00:00Z</code>).
      </p>
     </p></blockquote>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetimeimmutable.construct-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns a new DateTimeImmutable instance.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-datetimeimmutable.construct-errors">
  <h3 class="title">Errori/Eccezioni</h3>
  <p class="para">
    If an invalid Date/Time string is passed,
    <span class="exceptionname"><a href="class.datemalformedstringexception.php" class="exceptionname">DateMalformedStringException</a></span> is thrown.
    Previous 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-datetimeimmutable.construct-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Versione</th>
       <th>Descrizione</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.3.0</td>
       <td>
        Now throws
        <span class="exceptionname"><a href="class.datemalformedstringexception.php" class="exceptionname">DateMalformedStringException</a></span> if an
        invalid string is passed, instead of
        <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>From now on microseconds are filled with actual value. Not with &#039;00000&#039;.</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetimeimmutable.construct-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="function"><strong>DateTimeImmutable::__construct()</strong></span> example</strong></p>
   <div class="example-contents"><p>Stile orientato agli oggetti</p></div>
   <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: #007700">try {<br />    </span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">);<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />    exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">);</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2000-01-01</pre>
</div>
   </div>
   <div class="example-contents"><p>Stile procedurale</p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$date </span><span style="color: #007700">= </span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$date</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">date_get_last_errors</span><span style="color: #007700">();<br />    foreach (</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'errors'</span><span style="color: #007700">] as </span><span style="color: #0000BB">$error</span><span style="color: #007700">) {<br />        echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$error</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />    }<br />    exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br />echo </span><span style="color: #0000BB">date_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">, </span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">);</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2000-01-01</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-2">
   <p><strong>Example #2 Intricacies of <span class="function"><strong>DateTimeImmutable::__construct()</strong></span></strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />date_default_timezone_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'America/Jamaica'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Specified date/time in PHP's default time zone.<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d H:i:sP'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Specified date/time in the specified time zone.<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-01-01'</span><span style="color: #007700">, new </span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pacific/Nauru'</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d H:i:sP'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Current date/time in your PHP's default time zone.<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d H:i:sP'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Current date/time in the specified time zone.<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'now'</span><span style="color: #007700">, new </span><span style="color: #0000BB">DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pacific/Nauru'</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d H:i:sP'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Using a UNIX timestamp.  Notice the result is in the UTC time zone.<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'@946684800'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d H:i:sP'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Non-existent values roll over.<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-02-30'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d H:i:sP'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà
qualcosa simile a:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2000-01-01 00:00:00-05:00
2000-01-01 00:00:00+12:00
2010-04-24 10:24:16-04:00
2010-04-25 02:24:16+12:00
2000-01-01 00:00:00+00:00
2000-03-01 00:00:00-05:00</pre>
</div>
   </div>
   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <p class="para">
     Rolled over dates can be detected by checking for warnings using
     <span class="function"><a href="datetimeimmutable.getlasterrors.php" class="function">DateTimeImmutable::getLastErrors()</a></span>.
    </p>
   </p></blockquote>
  </div>

  <div class="example" id="example-3">
   <p><strong>Example #3 Changing the associated timezone</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$timeZone </span><span style="color: #007700">= new </span><span style="color: #0000BB">\DateTimeZone</span><span style="color: #007700">(</span><span style="color: #DD0000">'Asia/Tokyo'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$time </span><span style="color: #007700">= new </span><span style="color: #0000BB">\DateTimeImmutable</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$time </span><span style="color: #007700">= </span><span style="color: #0000BB">$time</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setTimezone</span><span style="color: #007700">(</span><span style="color: #0000BB">$timeZone</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$time</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y/m/d H:i:s e'</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà
qualcosa simile a:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2022/08/12 23:49:23 Asia/Tokyo</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-4">
   <p><strong>Example #4 Using a relative date/time string</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$time </span><span style="color: #007700">= new </span><span style="color: #0000BB">\DateTimeImmutable</span><span style="color: #007700">(</span><span style="color: #DD0000">"-1 year"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$time</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y/m/d H:i:s'</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Il precedente esempio visualizzerà
qualcosa simile a:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">2021/08/12 15:43:51</pre>
</div>
   </div>
  </div>
 </div>



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