<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.datetime.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.strtotime.php',
    1 => 'strtotime',
    2 => '将任何英文文本日期时间描述解析为 Unix 时间戳',
  ),
  'up' => 
  array (
    0 => 'ref.datetime.php',
    1 => 'Date/Time 函数',
  ),
  'prev' => 
  array (
    0 => 'function.strptime.php',
    1 => 'strptime',
  ),
  'next' => 
  array (
    0 => 'function.time.php',
    1 => 'time',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/datetime/functions/strtotime.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.strtotime" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strtotime</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strtotime</span> &mdash; <span class="dc-title">将任何英文文本日期时间描述解析为 Unix 时间戳</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.strtotime-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strtotime</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>, <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="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$baseTimestamp</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="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">
   本函数期望接受包含英文日期格式的字符串并尝试将其解析为
   Unix 时间戳（自 January 1 1970 00:00:00 UTC 起的秒数），其值跟
   <code class="parameter">baseTimestamp</code> 参数给出的时间相关，如果没有提供
   <code class="parameter">baseTimestamp</code> 参数则为当前时间。在<a href="datetime.formats.php" class="link">日期和时间格式</a>中定义了日期字符串解析，并且有几个微秒的注意事项，强烈建议查看完整详细信息。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    本函数返回的 Unix 时间戳不包含时区信息，为了实现对日期/时间信息进行计算，推荐使用功能更强大的
    <span class="classname"><a href="class.datetimeimmutable.php" class="classname">DateTimeImmutable</a></span>。
   </p>
  </div>
  <p class="para">
   本函数的每个参数都使用默认时区，除非在该参数指定了时区。注意不要在每个参数中使用不同的时区，除非是故意的。参考
   <span class="function"><a href="function.date-default-timezone-get.php" class="function">date_default_timezone_get()</a></span> 获取定义默认时区的各种方法。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strtotime-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">datetime</code></dt>
     <dd>
      <p class="para">日期/时间字符串。正确格式的说明详见 <a href="datetime.formats.php" class="link">日期与时间格式</a>。</p>
     </dd>
    
    
     <dt><code class="parameter">baseTimestamp</code></dt>
     <dd>
      <p class="para">
       时间戳，用作计算相对日期的基础。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.strtotime-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功则返回时间戳，否则返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>

 
 <div class="refsect1 errors" id="refsect1-function.strtotime-errors">
  <h3 class="title">错误／异常</h3>
   
   <p class="para">
在每次调用日期/时间函数时，如果时区无效则会引发 <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> 错误。参见
<span class="function"><a href="function.date-default-timezone-set.php" class="function">date_default_timezone_set()</a></span>。</p>
 
 </div>


 <div class="refsect1 changelog" id="refsect1-function.strtotime-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.0.0</td>
       <td>
        现在 <code class="parameter">baseTimestamp</code> 允许为 null。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strtotime-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>strtotime()</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 /></span><span style="color: #007700">echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"now"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"10 September 2000"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 day"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 week"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"+1 week 2 days 4 hours 2 seconds"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"next Thursday"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #DD0000">"last Monday"</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 失败检查</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Not Good'</span><span style="color: #007700">;<br /><br />if ((</span><span style="color: #0000BB">$timestamp </span><span style="color: #007700">= </span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)) === </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"The string (</span><span style="color: #0000BB">$str</span><span style="color: #DD0000">) is bogus"</span><span style="color: #007700">;<br />} else {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$str</span><span style="color: #DD0000"> == " </span><span style="color: #007700">. </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'l dS \o\f F Y h:i:s A'</span><span style="color: #007700">, </span><span style="color: #0000BB">$timestamp</span><span style="color: #007700">);<br />}</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.strtotime-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在这种情况下，“相对”日期还意味着，如果未提供日期/时间的特定组成部分，则将从 <code class="parameter">baseTimestamp</code>
    逐字获取。也就是说如果在 2022 年 5 月 31 日运行 <code class="code">strtotime(&#039;February&#039;)</code>，将被解释为
    <code class="literal">31 February 2022</code>，它将溢出到 <code class="literal">3 March</code> 的时间戳。（在闰年，应该是
    <code class="literal">2 March</code>。）使用 <code class="code">strtotime(&#039;1 February&#039;)</code> 或者
    <code class="code">strtotime(&#039;first day of February&#039;)</code> 可以避免这个问题。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果指定的年份位数是两位数字，则其值 0-69 表示 2000-2069，70-99 表示 1970-1999。参阅下面的注释了解
    32 位系统上可能的差异（结束日期可能是 2038-01-19 03:14:07）。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    时间戳的有效范围通常从 Fri, 13 Dec 1901 20:45:54 UTC
    到 Tue, 19 Jan 2038 03:14:07 UTC（这些日期对应 32
    位有符号整数的最小值和最大值）。
   </p>
   <p class="para">
    在 64 位的 PHP 版本中，时间戳的有效范围实际上是无限的，因为
    64 位在任一方向上都可以代表大约 2930 亿年。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    不建议使用此函数对日期进行数学运算。推荐使用
    <span class="methodname"><a href="datetime.add.php" class="methodname">DateTime::add()</a></span> 和
    <span class="methodname"><a href="datetime.sub.php" class="methodname">DateTime::sub()</a></span> 函数。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.strtotime-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="classname"><a href="class.datetimeimmutable.php" class="classname">DateTimeImmutable</a></span></li>
    <li><span class="methodname"><a href="datetimeimmutable.createfromformat.php" class="methodname" rel="rdfs-seeAlso">DateTimeImmutable::createFromFormat()</a> - Parses a time string according to a specified format</span></li>
    <li><a href="datetime.formats.php" class="link">日期和时间格式</a></li>
    <li><span class="function"><a href="function.checkdate.php" class="function" rel="rdfs-seeAlso">checkdate()</a> - 验证一个格里高里日期</span></li>
    <li><span class="function"><a href="function.strptime.php" class="function" rel="rdfs-seeAlso">strptime()</a> - 解析由 strftime 生成的日期／时间</span></li>
   </ul>
  </p>
 </div>

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