<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'datetimeimmutable.createfromformat.php',
    1 => 'DateTimeImmutable::createFromFormat',
    2 => 'Parses a time string according to a specified format',
  ),
  'up' => 
  array (
    0 => 'class.datetimeimmutable.php',
    1 => 'DateTimeImmutable',
  ),
  'prev' => 
  array (
    0 => 'datetimeimmutable.construct.php',
    1 => 'DateTimeImmutable::__construct',
  ),
  'next' => 
  array (
    0 => 'datetimeimmutable.createfrominterface.php',
    1 => 'DateTimeImmutable::createFromInterface',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/datetime/datetimeimmutable/createfromformat.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="datetimeimmutable.createfromformat" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTimeImmutable::createFromFormat</h1>
  <h1 class="refname">date_create_immutable_from_format</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateTimeImmutable::createFromFormat</span> -- <span class="refname">date_create_immutable_from_format</span> &mdash; <span class="dc-title">Parses a time string according to a specified format</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetimeimmutable.createfromformat-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>DateTimeImmutable::createFromFormat</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$format</code></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="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">过程化风格</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><a href="function.date-create-immutable-from-format.php" class="methodname">date_create_immutable_from_format</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$format</code></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="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 a new DateTimeImmutable object representing the date and time specified by the
   <code class="parameter">datetime</code> string, which was formatted in the given
   <code class="parameter">format</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="datetimeimmutable.createfromformat.parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">format</code></dt>
    <dd>
     <p class="para">
      The format that the passed in <span class="type"><a href="language.types.string.php" class="type string">string</a></span> should be in. See the
      formatting options below. In most cases, the same letters as for the
      <span class="function"><a href="function.date.php" class="function">date()</a></span> can be used.
     </p>
     <p class="para">
      All fields are initialised with the current date/time. In most cases you
      would want to reset these to &quot;zero&quot; (the Unix epoch, <code class="literal">1970-01-01
      00:00:00 UTC</code>). You do that by including the
      <code class="literal">!</code> character as first character in your
      <code class="parameter">format</code>, or <code class="literal">|</code> as your last.
      Please see the documentation for each character below for more
      information.
     </p>
     <p class="para">
      The format is parsed from left to right, which means that in some
      situations the order in which the format characters are present affects
      the result. In the case of <code class="literal">z</code> (the day of the year),
      it is required that a year has already been parsed,
      for example through the <code class="literal">Y</code> or <code class="literal">y</code>
      characters.
     </p>
     <p class="para">
      Letters that are used for parsing numbers allow a wide range of values,
      outside of what the logical range would be. For example, the
      <code class="literal">d</code> (day of the month) accepts values in the range from
      <code class="literal">00</code> to <code class="literal">99</code>. The only constraint is
      on the amount of digits. The date/time parser&#039;s overflow mechanism is
      used when out-of-range values are given. The examples below show some of
      this behaviour.
     </p>
     <p class="para">
      This also means that the data parsed for a format letter is greedy, and
      will read up to the amount of digits its format allows for. That can
      then also mean that there are no
      longer enough characters in the <code class="parameter">datetime</code> string
      for following format characters. An example on this page also
      illustrates this issue.
     </p>
     <p class="para">
      <table class="doctable table">
       <caption><strong>The following characters are recognized in the
       <code class="parameter">format</code> parameter string</strong></caption>
       
        <thead>
         <tr>
          <th><code class="parameter">format</code> character</th>
          <th>Description</th>
          <th>Example parsable values</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td style="text-align: center;"><em>Day</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><code class="literal">d</code> and <code class="literal">j</code></td>
          <td>Day of the month, 2 digits with or without leading zeros</td>
          <td>
           <code class="literal">01</code> to <code class="literal">31</code> or
           <code class="literal">1</code> to <code class="literal">31</code>. (2 digit numbers
           higher than the number of days in the month are accepted, in which
           case they will make the month overflow. For example using 33 with
           January, means February 2nd)
          </td>
         </tr>

         <tr>
          <td><code class="literal">D</code> and <code class="literal">l</code></td>
          <td>A textual representation of a day</td>
          <td>
           <code class="literal">Mon</code> through <code class="literal">Sun</code> or
           <code class="literal">Sunday</code> through <code class="literal">Saturday</code>. If
           the day name given is different then the day name belonging to a
           parsed (or default) date is different, then an overflow occurs to
           the <em>next</em> date with the given day name. See the
           examples below for an explanation.
          </td>
         </tr>

         <tr>
          <td><code class="literal">S</code></td>
          <td>English ordinal suffix for the day of the month, 2
          characters. It&#039;s ignored while processing.</td>
          <td>
           <code class="literal">st</code>, <code class="literal">nd</code>, <code class="literal">rd</code> or
           <code class="literal">th</code>.
          </td>
         </tr>

         <tr>
          <td><code class="literal">z</code></td>
          <td>
           The day of the year (starting from 0);
           must be preceded by <code class="literal">Y</code> or <code class="literal">y</code>.
          </td>
          <td>
           <code class="literal">0</code> through <code class="literal">365</code>. (3 digit
           numbers higher than the numbers in a year are accepted, in which
           case they will make the year overflow. For example using 366 with
           2022, means January 2nd, 2023)
          </td>
         </tr>

         <tr>
          <td style="text-align: center;"><em>Month</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><code class="literal">F</code> and <code class="literal">M</code></td>
          <td>A textual representation of a month, such as January or Sept</td>
          <td>
           <code class="literal">January</code> through <code class="literal">December</code> or
           <code class="literal">Jan</code> through <code class="literal">Dec</code>
          </td>
         </tr>

         <tr>
          <td><code class="literal">m</code> and <code class="literal">n</code></td>
          <td>Numeric representation of a month, with or without leading zeros</td>
          <td>
           <code class="literal">01</code> through <code class="literal">12</code> or
           <code class="literal">1</code> through <code class="literal">12</code>.
           (2 digit numbers higher than 12 are accepted, in which case they
           will make the year overflow. For example using 13 means January in
           the next year)
          </td>
         </tr>

         <tr>
          <td style="text-align: center;"><em>Year</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><code class="literal">X</code> and <code class="literal">x</code></td>
          <td>A full numeric representation of a year, <em>up to</em> 19 digits,
           optionally prefixed by <code class="literal">+</code> or
           <code class="literal">-</code></td>
          <td>Examples: <code class="literal">0055</code>, <code class="literal">787</code>,
           <code class="literal">1999</code>, <code class="literal">-2003</code>,
           <code class="literal">+10191</code></td>
         </tr>

         <tr>
          <td><code class="literal">Y</code></td>
          <td>A full numeric representation of a year, <em>up to</em> 4 digits</td>
          <td>Examples: <code class="literal">25</code> (same as <code class="literal">0025</code>),
           <code class="literal">787</code>, <code class="literal">1999</code>, <code class="literal">2003</code></td>
         </tr>

         <tr>
          <td><code class="literal">y</code></td>
          <td>
           A two digit representation of a year (which is assumed to be in the
           range 1970-2069, inclusive)
          </td>
          <td>
           Examples:
           <code class="literal">99</code> or <code class="literal">03</code>
           (which will be interpreted as <code class="literal">1999</code> and
           <code class="literal">2003</code>, respectively)
          </td>
         </tr>

         <tr>
          <td style="text-align: center;"><em>Time</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><code class="literal">a</code> and <code class="literal">A</code></td>
          <td>Ante meridiem and Post meridiem</td>
          <td><code class="literal">am</code> or <code class="literal">pm</code></td>
         </tr>

         <tr>
          <td><code class="literal">g</code> and <code class="literal">h</code></td>
          <td>12-hour format of an hour with or without leading zero</td>
          <td>
           <code class="literal">1</code> through <code class="literal">12</code> or
           <code class="literal">01</code> through <code class="literal">12</code> (2 digit
           numbers higher than 12 are accepted, in which case they will make
           the day overflow. For example using <code class="literal">14</code> means
           <code class="literal">02</code> in the next AM/PM period)
          </td>
         </tr>

         <tr>
          <td><code class="literal">G</code> and <code class="literal">H</code></td>
          <td>24-hour format of an hour with or without leading zeros</td>
          <td>
           <code class="literal">0</code> through <code class="literal">23</code> or
           <code class="literal">00</code> through <code class="literal">23</code> (2 digit
           numbers higher than 24 are accepted, in which case they will make
           the day overflow. For example using <code class="literal">26</code> means
           <code class="literal">02:00</code> the next day)
          </td>
         </tr>

         <tr>
          <td><code class="literal">i</code></td>
          <td>Minutes with leading zeros</td>
          <td>
           <code class="literal">00</code> to <code class="literal">59</code>. (2 digit
           numbers higher than 59 are accepted, in which case they will make
           the hour overflow. For example using <code class="literal">66</code> means
           <code class="literal">:06</code> the next hour)
          </td>
         </tr>

         <tr>
          <td><code class="literal">s</code></td>
          <td>Seconds, with leading zeros</td>
          <td>
           <code class="literal">00</code> through <code class="literal">59</code> (2 digit
           numbers higher than 59 are accepted, in which case they will make
           the minute overflow. For example using <code class="literal">90</code> means
           <code class="literal">:30</code> the next minute)
          </td>
         </tr>

         <tr>
          <td><code class="literal">v</code></td>
          <td>Fraction in milliseconds (up to three digits)</td>
          <td>Example: <code class="literal">12</code> (<code class="literal">0.12</code>
          seconds), <code class="literal">345</code> (<code class="literal">0.345</code> seconds)</td>
         </tr>

         <tr>
          <td><code class="literal">u</code></td>
          <td>Fraction in microseconds (up to six digits)</td>
          <td>Example: <code class="literal">45</code> (<code class="literal">0.45</code>
          seconds), <code class="literal">654321</code> (<code class="literal">0.654321</code>
          seconds)</td>
         </tr>

         <tr>
          <td style="text-align: center;"><em>Timezone</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td>
           <code class="literal">e</code>, <code class="literal">O</code>, <code class="literal">p</code>,
           <code class="literal">P</code> and <code class="literal">T</code>
          </td>
          <td>Timezone identifier, or difference to UTC in hours, or
          difference to UTC with colon between hours and minutes, or timezone
          abbreviation</td>
          <td>Examples: <code class="literal">UTC</code>, <code class="literal">GMT</code>,
           <code class="literal">Atlantic/Azores</code> or
           <code class="literal">+0200</code> or <code class="literal">+02:00</code> or
           <code class="literal">EST</code>, <code class="literal">MDT</code>
          </td>
         </tr>

         <tr>
          <td style="text-align: center;"><em>Full Date/Time</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><code class="literal">U</code></td>
          <td>Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)</td>
          <td>Example: <code class="literal">1292177455</code></td>
         </tr>

         <tr>
          <td style="text-align: center;"><em>Whitespace and Separators</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><code class="literal"> </code> (space)</td>
          <td>Zero or more spaces, tabs, NBSP (U+A0), or NNBSP (U+202F)
          characters</td>
          <td>Example: <code class="literal">&quot;\t&quot;</code>, <code class="literal">&quot;  &quot;</code></td>
         </tr>

         <tr>
          <td><code class="literal">#</code></td>
          <td>
           One of the following separation symbol: <code class="literal">;</code>,
           <code class="literal">:</code>, <code class="literal">/</code>, <code class="literal">.</code>,
           <code class="literal">,</code>, <code class="literal">-</code>, <code class="literal">(</code> or
           <code class="literal">)</code>
          </td>
          <td>Example: <code class="literal">/</code></td>
         </tr>

         <tr>
          <td>
           <code class="literal">;</code>,
           <code class="literal">:</code>, <code class="literal">/</code>, <code class="literal">.</code>,
           <code class="literal">,</code>, <code class="literal">-</code>, <code class="literal">(</code> or
           <code class="literal">)</code>
          </td>
          <td>The specified character.</td>
          <td>Example: <code class="literal">-</code></td>
         </tr>

         <tr>
          <td><code class="literal">?</code></td>
          <td>A random byte</td>
          <td>Example: <code class="literal">^</code> (Be aware that for UTF-8
          characters you might need more than one <code class="literal">?</code>.
          In this case, using <code class="literal">*</code> is probably what you want
          instead)</td>
         </tr>

         <tr>
          <td><code class="literal">*</code></td>
          <td>Random bytes until the next separator or digit</td>
          <td>Example: <code class="literal">*</code> in <code class="literal">Y-*-d</code> with
          the string <code class="literal">2009-aWord-08</code> will match
          <code class="literal">aWord</code></td>
         </tr>

         <tr>
          <td><code class="literal">!</code></td>
          <td>Resets all fields (year, month, day, hour, minute, second,
          fraction and timezone information) to zero-like values (
           <code class="literal">0</code> for hour, minute, second and fraction,
           <code class="literal">1</code> for month and day, <code class="literal">1970</code>
           for year and the default timezone)</td>
          <td>Without <code class="literal">!,</code> all fields will be set to the
          current date and time.</td>
         </tr>

         <tr>
          <td><code class="literal">|</code></td>
          <td>Resets all fields (year, month, day, hour, minute, second,
          fraction and timezone information) to zero-like values if they have
          not been parsed yet</td>
          <td><code class="literal">Y-m-d|</code> will set the year, month and day
          to the information found in the string to parse, and sets the hour,
          minute and second to 0.</td>
         </tr>

         <tr>
          <td><code class="literal">+</code></td>
          <td>If this format specifier is present, trailing data in the
          string will not cause an error, but a warning instead</td>
          <td>Use <span class="methodname"><a href="datetimeimmutable.getlasterrors.php" class="methodname">DateTimeImmutable::getLastErrors()</a></span> to find out
          whether trailing data was present.</td>
         </tr>

        </tbody>
       
      </table>

     </p>
     <p class="para">
      Unrecognized characters in the format string will cause the
      parsing to fail and an error message is appended to the returned
      structure. You can query error messages with
      <span class="methodname"><a href="datetimeimmutable.getlasterrors.php" class="methodname">DateTimeImmutable::getLastErrors()</a></span>.
     </p>
     <p class="para">
      To include literal characters in <code class="parameter">format</code>, you have
      to escape them with a backslash (<code class="literal">\</code>).
     </p>
     <p class="para">
      If <code class="parameter">format</code> does not contain the character
      <code class="literal">!</code> then portions of the generated date/time which are not
      specified in <code class="parameter">format</code> will be set to the current
      system time.
     </p>
     <p class="para">
      If <code class="parameter">format</code> contains the
      character <code class="literal">!</code>, then portions of the generated
      date/time not provided in <code class="parameter">format</code>, as well as
      values to the left-hand side of the <code class="literal">!</code>, will
      be set to corresponding values from the Unix epoch.
     </p>
     <p class="para">
      If any time character is parsed, then all other time-related fields are
      set to &quot;0&quot;, unless also parsed.
     </p>
     <p class="para">
      The Unix epoch is 1970-01-01 00:00:00 UTC.
     </p>
    </dd>
   
   
    <dt><code class="parameter">datetime</code></dt>
    <dd>
     <p class="para">
      String representing the time.
     </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
      desired time zone.
     </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> and
      <code class="parameter">datetime</code> contains no timezone,
      the current timezone will be used.
     </p>
     <blockquote class="note"><p><strong class="note">注意</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
       contains 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>).
      </p>
     </p></blockquote>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetimeimmutable.createfromformat-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a new DateTimeImmutable instance 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-datetimeimmutable.createfromformat-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   This method throws <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> when the
   <code class="parameter">datetime</code> contains NULL-bytes.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-datetimeimmutable.createfromformat-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.2.9</td>
       <td>
        The <code class="literal"> </code> (space) specifier now also supports NBSP
        (U+A0) and NNBSP (U+202F) characters.
       </td>
      </tr>

      <tr>
       <td>8.2.0</td>
       <td>
        The <code class="literal">X</code> and <code class="literal">x</code>
        <code class="parameter">format</code> specifiers have been added.
       </td>
      </tr>

      <tr>
       <td>8.0.21, 8.1.8, 8.2.0</td>
       <td>
        Now throws <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> when NULL-bytes
        are passed into <code class="parameter">datetime</code>, which previously was silently
        ignored.
       </td>
      </tr>

      <tr>
       <td>7.3.0</td>
       <td>
        The <code class="literal">v</code> <code class="parameter">format</code> specifier has
        been added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetimeimmutable.createfromformat-examples">
  <h3 class="title">示例</h3>
  <div class="example" id="example-1">
   <p><strong>示例 #1 <span class="function"><strong>DateTimeImmutable::createFromFormat()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</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">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">'j-M-Y'</span><span style="color: #007700">, </span><span style="color: #DD0000">'15-Feb-2009'</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'</span><span style="color: #007700">);</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-2">
   <p><strong>示例 #2 Using predefined format constants with <span class="function"><strong>DateTimeImmutable::createFromFormat()</strong></span></strong></p>
   <div class="example-contents"><p>面向对象风格</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">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(<br />    </span><span style="color: #0000BB">DateTimeInterface</span><span style="color: #007700">::</span><span style="color: #0000BB">ISO8601</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'2004-02-12T15:19:21+00:00'<br /></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">'c e'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(<br />    </span><span style="color: #0000BB">DateTimeInterface</span><span style="color: #007700">::</span><span style="color: #0000BB">RFC3339_EXTENDED</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'2013-10-14T09:00:00.000+02:00'<br /></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">'c 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>
    The <a href="class.datetimeinterface.php#datetimeinterface.constants.types" class="link">formatting constants</a>
    as used in this example consist of
    a string of characters for <a href="datetime.format.php" class="link">formatting</a> a
    <span class="classname"><a href="class.datetimeimmutable.php" class="classname">DateTimeImmutable</a></span> object. In most cases, these
    letters match with the same elements of date/time information as the ones defined in the <a href="" class="link">parameters</a>
    section above, but they tend to be more lenient.
   </p></div>
  </div>

  <div class="example" id="example-3">
   <p><strong>示例 #3 Intricacies of <span class="function"><strong>DateTimeImmutable::createFromFormat()</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: #DD0000">'Current time: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">date</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">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #DD0000">'2009-02-15'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Format: </span><span style="color: #0000BB">$format</span><span style="color: #DD0000">; " </span><span style="color: #007700">. </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:s'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></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">;<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #DD0000">'2009-02-15 15:16:17'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Format: </span><span style="color: #0000BB">$format</span><span style="color: #DD0000">; " </span><span style="color: #007700">. </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:s'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></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">;<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #DD0000">'2009-02-15 15:16:17'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Format: </span><span style="color: #0000BB">$format</span><span style="color: #DD0000">; " </span><span style="color: #007700">. </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:s'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'!d'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #DD0000">'15'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Format: </span><span style="color: #0000BB">$format</span><span style="color: #DD0000">; " </span><span style="color: #007700">. </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:s'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format </span><span style="color: #007700">= </span><span style="color: #DD0000">'i'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #DD0000">'15'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Format: </span><span style="color: #0000BB">$format</span><span style="color: #DD0000">; " </span><span style="color: #007700">. </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: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>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Current time: 2022-06-02 15:50:46
Format: Y-m-d; 2009-02-15 15:50:46
Format: Y-m-d H:i:s; 2009-02-15 15:16:17
Format: Y-m-!d H:i:s; 1970-01-15 15:16:17
Format: !d; 1970-01-15 00:00:00
Format: i; 2022-06-02 00:15:00</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-4">
   <p><strong>示例 #4 Format string with literal characters</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">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">'H\h i\m s\s'</span><span style="color: #007700">,</span><span style="color: #DD0000">'23h 15m 03s'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'H:i:s'</span><span style="color: #007700">);</span></span></code></div>
   </div>

   <div class="example-contents"><p>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">23:15:03</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-5">
   <p><strong>示例 #5 Overflow behaviour</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">DateTimeImmutable</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:s'</span><span style="color: #007700">, </span><span style="color: #DD0000">'2021-17-35 16:60:97'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">RFC2822</span><span style="color: #007700">);</span></span></code></div>
   </div>

   <div class="example-contents"><p>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Sat, 04 Jun 2022 17:01:37 +0000</pre>
</div>
   </div>
   <div class="example-contents"><p>
    Although the result looks odd, it is correct, as the following overflows
    happen:
   </p></div>
   <ol type="1">
    <li class="listitem">
     <span class="simpara">
      <code class="literal">97</code> seconds overflows to <code class="literal">1</code> minute,
      leaving <code class="literal">37</code> seconds.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <code class="literal">61</code> minutes overflows to <code class="literal">1</code> hour,
      leaving <code class="literal">1</code> minutes.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <code class="literal">35</code> days overflows to <code class="literal">1</code> month,
      leaving <code class="literal">4</code> days. The amount of days that are left over
      depends on the month, as not every month has the same amount of days.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <code class="literal">18</code> months overflows to <code class="literal">1</code> year,
      leaving <code class="literal">6</code> months.
     </span>
    </li>
   </ol>
  </div>

  <div class="example" id="example-6">
   <p><strong>示例 #6 Overflowing day name behaviour</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$d </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: #0000BB">DateTimeInterface</span><span style="color: #007700">::</span><span style="color: #0000BB">RFC1123</span><span style="color: #007700">, </span><span style="color: #DD0000">'Mon, 3 Aug 2020 25:00:00 +0000'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">RFC1123</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>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Mon, 10 Aug 2020 01:00:00 +0000</pre>
</div>
   </div>
   <div class="example-contents"><p>
    Although the result looks odd, it is correct, as the following overflows
    happen:
   </p></div>
   <ol type="1">
    <li class="listitem">
     <span class="simpara">
      <code class="literal">3 Aug 2020 25:00:00</code> overflows to <code class="literal">(Tue) 4 Aug
      2020 01:00</code>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <code class="literal">Mon</code> gets applied, which advances the date to
      <code class="literal">Mon, 10 Aug 2020 01:00:00</code>. The explanation of
      relative keywords such as <code class="literal">Mon</code> is explained in the
      section on <a href="datetime.formats.php#datetime.formats.relative" class="link">relative
      formats</a>.
     </span>
    </li>
   </ol>
  </div>

  <p class="para">
   In order to detect overflows in dates, you can use
   <span class="methodname"><a href="datetimeimmutable.getlasterrors.php" class="methodname">DateTimeImmutable::getLastErrors()</a></span>, which will
   include a warning if an overflow occurred.
  </p>
  <div class="example" id="example-7">
   <p><strong>示例 #7 Detecting overflown dates</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$d </span><span style="color: #007700">= </span><span style="color: #0000BB">DateTimeImmutable</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:s'</span><span style="color: #007700">, </span><span style="color: #DD0000">'2021-17-35 16:60:97'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">RFC2822</span><span style="color: #007700">), </span><span style="color: #DD0000">"\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">DateTimeImmutable</span><span style="color: #007700">::</span><span style="color: #0000BB">getLastErrors</span><span style="color: #007700">());</span></span></code></div>
   </div>

   <div class="example-contents"><p>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Sat, 04 Jun 2022 17:01:37 +0000

array(4) {
  &#039;warning_count&#039; =&gt;
  int(2)
  &#039;warnings&#039; =&gt;
  array(1) {
    [19] =&gt;
    string(27) &quot;The parsed date was invalid&quot;
  }
  &#039;error_count&#039; =&gt;
  int(0)
  &#039;errors&#039; =&gt;
  array(0) {
  }
}</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-8">
   <p><strong>示例 #8 Greedy parsing behaviour</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">date_parse_from_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Gis'</span><span style="color: #007700">, </span><span style="color: #DD0000">'60101'</span><span style="color: #007700">));</span></span></code></div>
   </div>

   <div class="example-contents"><p>以上示例的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [year] =&gt;
    [month] =&gt;
    [day] =&gt;
    [hour] =&gt; 60
    [minute] =&gt; 10
    [second] =&gt; 0
    [fraction] =&gt; 0
    [warning_count] =&gt; 1
    [warnings] =&gt; Array
        (
            [5] =&gt; The parsed time was invalid
        )

    [error_count] =&gt; 1
    [errors] =&gt; Array
        (
            [4] =&gt; A two digit second could not be found
        )

    [is_localtime] =&gt;
)</pre>
</div>
   </div>
   <div class="example-contents"><p>
    The <code class="literal">G</code> format is to parse 24 hour clock hours, with or
    without leading zero. This requires to parse 1 or 2 digits. Because there
    are two following digits, it greedily reads this as <code class="literal">60</code>.
   </p></div>
   <div class="example-contents"><p>
    The following <code class="literal">i</code> and <code class="literal">s</code> format
    characters both require two digits. This means that <code class="literal">10</code>
    is passed as minute (<code class="literal">i</code>), and that there are then not
    enough digits left to parse for as second (<code class="literal">s</code>).
   </p></div>
   <div class="example-contents"><p>
    The <code class="literal">errors</code> array indicates this problem.
   </p></div>
   <div class="example-contents"><p>
    Additionally, an hour of <code class="literal">60</code> is outside the range
    <code class="literal">0</code>-<code class="literal">24</code>, which causes the
    <code class="literal">warnings</code> array to include a warning that the time is
    invalid.
   </p></div>
  </div>

 </div>


 <div class="refsect1 seealso" id="refsect1-datetimeimmutable.createfromformat-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="datetimeimmutable.construct.php" class="function" rel="rdfs-seeAlso">DateTimeImmutable::__construct()</a> - Returns new DateTimeImmutable object</span></li>
   <li><span class="function"><a href="datetimeimmutable.getlasterrors.php" class="function" rel="rdfs-seeAlso">DateTimeImmutable::getLastErrors()</a> - Returns the warnings and errors</span></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>
 </div>


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