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

contributors($setup);

?>
<div id="datetime.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTime::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTime::__construct</span> &mdash; <span class="dc-title">返回新的 DateTime 对象</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetime.construct-description">
  <h3 class="title">说明</h3>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateTime::__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">
   跟 <span class="methodname"><a href="datetimeimmutable.construct.php" class="methodname">DateTimeImmutable::__construct()</a></span> 一样，但适用于
   <span class="classname"><a href="class.datetime.php" class="classname">DateTime</a></span>。Consider using the
   <span class="classname"><a href="class.datetimeimmutable.php" class="classname">DateTimeImmutable</a></span> and features instead.
  </p>
  <p class="para">
   返回新的 DateTime 对象。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetime.construct-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">datetime</code></dt>
    <dd>
     <p class="para">日期/时间字符串。正确格式的说明详见 <a href="datetime.formats.php" class="link">日期与时间格式</a>。</p>
     <p class="para">
      如果这个参数为字符串 <code class="literal">&quot;now&quot;</code> 表示获取当前时间。
      如果同时指定了 <code class="parameter">$timezone</code> 参数，那么获取指定时区的当前时间。
     </p>
    </dd>
   
   
    <dt><code class="parameter">timezone</code></dt>
    <dd>
     <p class="para">
      <span class="classname"><a href="class.datetimezone.php" class="classname">DateTimeZone</a></span> 对象表示
      <code class="parameter">$datetime</code> 的时区。
     </p>
     <p class="para">
      如果省略了 <code class="parameter">$timezone</code> 参数或者传递 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>，
      那么会使用当前时区。
     </p>
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <p class="para">
       当 <code class="parameter">$datetime</code> 参数是 UNIX 时间戳（例如
       <code class="literal">@946684800</code>），或者已经包含时区信息（例如 <code class="literal">2010-01-28T15:00:00+02:00</code>）的时候，<code class="parameter">$timezone</code>
       参数和当前时区都将被忽略。
      </p>
     </p></blockquote>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetime.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个新的 DateTime 对象实例，或者在发生错误的时候返回
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-datetime.construct-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
    如果传递无效的日期/时间字符串，将会抛出 <span class="exceptionname"><a href="class.datemalformedstringexception.php" class="exceptionname">DateMalformedStringException</a></span>，在
    PHP 8.3 之前，将抛出 <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-datetime.construct-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.3.0</td>
       <td>
        传递无效的字符串现在抛出 <span class="exceptionname"><a href="class.datemalformedstringexception.php" class="exceptionname">DateMalformedStringException</a></span>
        而不是 <span class="exceptionname"><a href="class.exception.php" class="exceptionname">Exception</a></span>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-datetime.construct-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="datetimeimmutable.construct.php" class="methodname" rel="rdfs-seeAlso">DateTimeImmutable::__construct()</a> - Returns new DateTimeImmutable object</span></li>
  </ul>
 </div>


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