1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="datetimeimmutable.createfromformat" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>DateTimeImmutable::createFromFormat</refname>
<refname>date_create_immutable_from_format</refname>
<refpurpose>Parses a time string according to a specified format</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>&style.oop;</para>
<methodsynopsis role="DateTimeImmutable">
<modifier>public</modifier> <modifier>static</modifier> <type class="union"><type>DateTimeImmutable</type><type>false</type></type><methodname>DateTimeImmutable::createFromFormat</methodname>
<methodparam><type>string</type><parameter>format</parameter></methodparam>
<methodparam><type>string</type><parameter>datetime</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>DateTimeZone</type><type>null</type></type><parameter>timezone</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>&style.procedural;</para>
<methodsynopsis>
<type class="union"><type>DateTimeImmutable</type><type>false</type></type><methodname>date_create_immutable_from_format</methodname>
<methodparam><type>string</type><parameter>format</parameter></methodparam>
<methodparam><type>string</type><parameter>datetime</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>DateTimeZone</type><type>null</type></type><parameter>timezone</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
Returns a new DateTimeImmutable object representing the date and time specified by the
<parameter>datetime</parameter> string, which was formatted in the given
<parameter>format</parameter>.
</para>
</refsect1>
<refsect1 role="parameters" xml:id="datetimeimmutable.createfromformat.parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>format</parameter></term>
<listitem>
<para>
The format that the passed in <type>string</type> should be in. See the
formatting options below. In most cases, the same letters as for the
<function>date</function> can be used.
</para>
<para>
All fields are initialised with the current date/time. In most cases you
would want to reset these to "zero" (the Unix epoch, <literal>1970-01-01
00:00:00 UTC</literal>). You do that by including the
<literal>!</literal> character as first character in your
<parameter>format</parameter>, or <literal>|</literal> as your last.
Please see the documentation for each character below for more
information.
</para>
<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 <literal>z</literal> (the day of the year),
it is required that a year has already been parsed,
for example through the <literal>Y</literal> or <literal>y</literal>
characters.
</para>
<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
<literal>d</literal> (day of the month) accepts values in the range from
<literal>00</literal> to <literal>99</literal>. The only constraint is
on the amount of digits. The date/time parser's overflow mechanism is
used when out-of-range values are given. The examples below show some of
this behaviour.
</para>
<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 <parameter>datetime</parameter> string
for following format characters. An example on this page also
illustrates this issue.
</para>
<para>
<table>
<title>The following characters are recognized in the
<parameter>format</parameter> parameter string</title>
<tgroup cols="3">
<thead>
<row>
<entry><parameter>format</parameter> character</entry>
<entry>Description</entry>
<entry>Example parsable values</entry>
</row>
</thead>
<tbody>
<row>
<entry align="center"><emphasis>Day</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>d</literal> and <literal>j</literal></entry>
<entry>Day of the month, 2 digits with or without leading zeros</entry>
<entry>
<literal>01</literal> to <literal>31</literal> or
<literal>1</literal> to <literal>31</literal>. (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)
</entry>
</row>
<row>
<entry><literal>D</literal> and <literal>l</literal></entry>
<entry>A textual representation of a day</entry>
<entry>
<literal>Mon</literal> through <literal>Sun</literal> or
<literal>Sunday</literal> through <literal>Saturday</literal>. 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 <emphasis>next</emphasis> date with the given day name. See the
examples below for an explanation.
</entry>
</row>
<row>
<entry><literal>S</literal></entry>
<entry>English ordinal suffix for the day of the month, 2
characters. It's ignored while processing.</entry>
<entry>
<literal>st</literal>, <literal>nd</literal>, <literal>rd</literal> or
<literal>th</literal>.
</entry>
</row>
<row>
<entry><literal>z</literal></entry>
<entry>
The day of the year (starting from 0);
must be preceded by <literal>Y</literal> or <literal>y</literal>.
</entry>
<entry>
<literal>0</literal> through <literal>365</literal>. (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)
</entry>
</row>
<row>
<entry align="center"><emphasis>Month</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>F</literal> and <literal>M</literal></entry>
<entry>A textual representation of a month, such as January or Sept</entry>
<entry>
<literal>January</literal> through <literal>December</literal> or
<literal>Jan</literal> through <literal>Dec</literal>
</entry>
</row>
<row>
<entry><literal>m</literal> and <literal>n</literal></entry>
<entry>Numeric representation of a month, with or without leading zeros</entry>
<entry>
<literal>01</literal> through <literal>12</literal> or
<literal>1</literal> through <literal>12</literal>.
(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)
</entry>
</row>
<row>
<entry align="center"><emphasis>Year</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>X</literal> and <literal>x</literal></entry>
<entry>A full numeric representation of a year, up to 19 digits,
optionally prefixed by <literal>+</literal> or
<literal>-</literal></entry>
<entry>Examples: <literal>0055</literal>, <literal>787</literal>,
<literal>1999</literal>, <literal>-2003</literal>,
<literal>+10191</literal></entry>
</row>
<row>
<entry><literal>Y</literal></entry>
<entry>A full numeric representation of a year, up to 4 digits</entry>
<entry>Examples: <literal>0055</literal>, <literal>787</literal>,
<literal>1999</literal>, <literal>2003</literal></entry>
</row>
<row>
<entry><literal>y</literal></entry>
<entry>
A two digit representation of a year (which is assumed to be in the
range 1970-2069, inclusive)
</entry>
<entry>
Examples:
<literal>99</literal> or <literal>03</literal>
(which will be interpreted as <literal>1999</literal> and
<literal>2003</literal>, respectively)
</entry>
</row>
<row>
<entry align="center"><emphasis>Time</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>a</literal> and <literal>A</literal></entry>
<entry>Ante meridiem and Post meridiem</entry>
<entry><literal>am</literal> or <literal>pm</literal></entry>
</row>
<row>
<entry><literal>g</literal> and <literal>h</literal></entry>
<entry>12-hour format of an hour with or without leading zero</entry>
<entry>
<literal>1</literal> through <literal>12</literal> or
<literal>01</literal> through <literal>12</literal> (2 digit
numbers higher than 12 are accepted, in which case they will make
the day overflow. For example using <literal>14</literal> means
<literal>02</literal> in the next AM/PM period)
</entry>
</row>
<row>
<entry><literal>G</literal> and <literal>H</literal></entry>
<entry>24-hour format of an hour with or without leading zeros</entry>
<entry>
<literal>0</literal> through <literal>23</literal> or
<literal>00</literal> through <literal>23</literal> (2 digit
numbers higher than 24 are accepted, in which case they will make
the day overflow. For example using <literal>26</literal> means
<literal>02:00</literal> the next day)
</entry>
</row>
<row>
<entry><literal>i</literal></entry>
<entry>Minutes with leading zeros</entry>
<entry>
<literal>00</literal> to <literal>59</literal>. (2 digit
numbers higher than 59 are accepted, in which case they will make
the hour overflow. For example using <literal>66</literal> means
<literal>:06</literal> the next hour)
</entry>
</row>
<row>
<entry><literal>s</literal></entry>
<entry>Seconds, with leading zeros</entry>
<entry>
<literal>00</literal> through <literal>59</literal> (2 digit
numbers higher than 59 are accepted, in which case they will make
the minute overflow. For example using <literal>90</literal> means
<literal>:30</literal> the next minute)
</entry>
</row>
<row>
<entry><literal>v</literal></entry>
<entry>Fraction in milliseconds (up to three digits)</entry>
<entry>Example: <literal>12</literal> (<literal>0.12</literal>
seconds), <literal>345</literal> (<literal>0.345</literal> seconds)</entry>
</row>
<row>
<entry><literal>u</literal></entry>
<entry>Fraction in microseconds (up to six digits)</entry>
<entry>Example: <literal>45</literal> (<literal>0.45</literal>
seconds), <literal>654321</literal> (<literal>0.654321</literal>
seconds)</entry>
</row>
<row>
<entry align="center"><emphasis>Timezone</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry>
<literal>e</literal>, <literal>O</literal>, <literal>p</literal>,
<literal>P</literal> and <literal>T</literal>
</entry>
<entry>Timezone identifier, or difference to UTC in hours, or
difference to UTC with colon between hours and minutes, or timezone
abbreviation</entry>
<entry>Examples: <literal>UTC</literal>, <literal>GMT</literal>,
<literal>Atlantic/Azores</literal> or
<literal>+0200</literal> or <literal>+02:00</literal> or
<literal>EST</literal>, <literal>MDT</literal>
</entry>
</row>
<row>
<entry align="center"><emphasis>Full Date/Time</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal>U</literal></entry>
<entry>Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)</entry>
<entry>Example: <literal>1292177455</literal></entry>
</row>
<row>
<entry align="center"><emphasis>Whitespace and Separators</emphasis></entry>
<entry>---</entry>
<entry>---</entry>
</row>
<row>
<entry><literal> </literal> (space)</entry>
<entry>Zero or more spaces, tabs, NBSP (U+A0), or NNBSP (U+202F)
characters</entry>
<entry>Example: <literal>"\t"</literal>, <literal>" "</literal></entry>
</row>
<row>
<entry><literal>#</literal></entry>
<entry>
One of the following separation symbol: <literal>;</literal>,
<literal>:</literal>, <literal>/</literal>, <literal>.</literal>,
<literal>,</literal>, <literal>-</literal>, <literal>(</literal> or
<literal>)</literal>
</entry>
<entry>Example: <literal>/</literal></entry>
</row>
<row>
<entry>
<literal>;</literal>,
<literal>:</literal>, <literal>/</literal>, <literal>.</literal>,
<literal>,</literal>, <literal>-</literal>, <literal>(</literal> or
<literal>)</literal>
</entry>
<entry>The specified character.</entry>
<entry>Example: <literal>-</literal></entry>
</row>
<row>
<entry><literal>?</literal></entry>
<entry>A random byte</entry>
<entry>Example: <literal>^</literal> (Be aware that for UTF-8
characters you might need more than one <literal>?</literal>.
In this case, using <literal>*</literal> is probably what you want
instead)</entry>
</row>
<row>
<entry><literal>*</literal></entry>
<entry>Random bytes until the next separator or digit</entry>
<entry>Example: <literal>*</literal> in <literal>Y-*-d</literal> with
the string <literal>2009-aWord-08</literal> will match
<literal>aWord</literal></entry>
</row>
<row>
<entry><literal>!</literal></entry>
<entry>Resets all fields (year, month, day, hour, minute, second,
fraction and timezone information) to zero-like values (
<literal>0</literal> for hour, minute, second and fraction,
<literal>1</literal> for month and day, <literal>1970</literal>
for year and <literal>UTC</literal> for timezone information)</entry>
<entry>Without <literal>!,</literal> all fields will be set to the
current date and time.</entry>
</row>
<row>
<entry><literal>|</literal></entry>
<entry>Resets all fields (year, month, day, hour, minute, second,
fraction and timezone information) to zero-like values if they have
not been parsed yet</entry>
<entry><literal>Y-m-d|</literal> 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.</entry>
</row>
<row>
<entry><literal>+</literal></entry>
<entry>If this format specifier is present, trailing data in the
string will not cause an error, but a warning instead</entry>
<entry>Use <methodname>DateTimeImmutable::getLastErrors</methodname> to find out
whether trailing data was present.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<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
<methodname>DateTimeImmutable::getLastErrors</methodname>.
</para>
<para>
To include literal characters in <parameter>format</parameter>, you have
to escape them with a backslash (<literal>\</literal>).
</para>
<para>
If <parameter>format</parameter> does not contain the character
<literal>!</literal> then portions of the generated date/time which are not
specified in <parameter>format</parameter> will be set to the current
system time.
</para>
<para>
If <parameter>format</parameter> contains the
character <literal>!</literal>, then portions of the generated
date/time not provided in <parameter>format</parameter>, as well as
values to the left-hand side of the <literal>!</literal>, will
be set to corresponding values from the Unix epoch.
</para>
<para>
If any time character is parsed, then all other time-related fields are
set to "0", unless also parsed.
</para>
<para>
The Unix epoch is 1970-01-01 00:00:00 UTC.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>datetime</parameter></term>
<listitem>
<para>
String representing the time.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>timezone</parameter></term>
<listitem>
<para>
A <classname>DateTimeZone</classname> object representing the
desired time zone.
</para>
<para>
If <parameter>timezone</parameter> is omitted or &null; and
<parameter>datetime</parameter> contains no timezone,
the current timezone will be used.
</para>
<note>
<para>
The <parameter>timezone</parameter> parameter
and the current timezone are ignored when the
<parameter>datetime</parameter> parameter either
contains a UNIX timestamp (e.g. <literal>946684800</literal>)
or specifies a timezone
(e.g. <literal>2010-01-28T15:00:00+02:00</literal>).
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a new DateTimeImmutable instance&return.falseforfailure;.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
This method throws <exceptionname>ValueError</exceptionname> when the
<parameter>datetime</parameter> contains NULL-bytes.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.2.9</entry>
<entry>
The <literal> </literal> (space) specifier now also supports NBSP
(U+A0) and NNBSP (U+202F) characters.
</entry>
</row>
<row>
<entry>8.2.0</entry>
<entry>
The <literal>X</literal> and <literal>x</literal>
<parameter>format</parameter> specifiers have been added.
</entry>
</row>
<row>
<entry>8.0.21, 8.1.8, 8.2.0</entry>
<entry>
Now throws <exceptionname>ValueError</exceptionname> when NULL-bytes
are passed into <parameter>datetime</parameter>, which previously was silently
ignored.
</entry>
</row>
<row>
<entry>7.3.0</entry>
<entry>
The <literal>v</literal> <parameter>format</parameter> specifier has
been added.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>DateTimeImmutable::createFromFormat</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = DateTimeImmutable::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
]]>
</programlisting>
</example>
<example>
<title>Using predefined format constants with <function>DateTimeImmutable::createFromFormat</function></title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2004-02-12T15:19:21+00:00');
$date = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, '2013-10-14T09:00:00.000+02:00');
?>
]]>
</programlisting>
<para>
The <link linkend="datetimeinterface.constants.types">formatting constants</link>
as used in this example consist of
a string of characters for <link linkend="datetime.format">formatting</link> a
<classname>DateTimeImmutable</classname> object. In most cases, these
letters match with the same elements of date/time information as the ones defined in the <link
linkend="datetimeimmutable.createfromformat.parameters">parameters</link>
section above, but they tend to be more lenient.
</para>
</example>
<example>
<title>Intricacies of <function>DateTimeImmutable::createFromFormat</function></title>
<programlisting role="php">
<![CDATA[
<?php
echo 'Current time: ' . date('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d';
$date = DateTimeImmutable::createFromFormat($format, '2009-02-15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-d H:i:s';
$date = DateTimeImmutable::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'Y-m-!d H:i:s';
$date = DateTimeImmutable::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = '!d';
$date = DateTimeImmutable::createFromFormat($format, '15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
$format = 'i';
$date = DateTimeImmutable::createFromFormat($format, '15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
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
]]>
</screen>
</example>
<example>
<title>Format string with literal characters</title>
<programlisting role="php">
<![CDATA[
<?php
echo DateTimeImmutable::createFromFormat('H\h i\m s\s','23h 15m 03s')->format('H:i:s');
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
23:15:03
]]>
</screen>
</example>
<example>
<title>Overflow behaviour</title>
<programlisting role="php">
<![CDATA[
<?php
echo DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2021-17-35 16:60:97')->format(DateTimeImmutable::RFC2822);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Sat, 04 Jun 2022 17:01:37 +0000
]]>
</screen>
<para>
Although the result looks odd, it is correct, as the following overflows
happen:
</para>
<orderedlist>
<listitem>
<simpara>
<literal>97</literal> seconds overflows to <literal>1</literal> minute,
leaving <literal>37</literal> seconds.
</simpara>
</listitem>
<listitem>
<simpara>
<literal>61</literal> minutes overflows to <literal>1</literal> hour,
leaving <literal>1</literal> minutes.
</simpara>
</listitem>
<listitem>
<simpara>
<literal>35</literal> days overflows to <literal>1</literal> month,
leaving <literal>4</literal> days. The amount of days that are left over
depends on the month, as not every month has the same amount of days.
</simpara>
</listitem>
<listitem>
<simpara>
<literal>18</literal> months overflows to <literal>1</literal> year,
leaving <literal>6</literal> months.
</simpara>
</listitem>
</orderedlist>
</example>
<example>
<title>Overflowing day name behaviour</title>
<programlisting role="php">
<![CDATA[
<?php
$d = DateTime::createFromFormat(DateTimeInterface::RFC1123, 'Mon, 3 Aug 2020 25:00:00 +0000');
echo $d->format(DateTime::RFC1123), "\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Mon, 10 Aug 2020 01:00:00 +0000
]]>
</screen>
<para>
Although the result looks odd, it is correct, as the following overflows
happen:
</para>
<orderedlist>
<listitem>
<simpara>
<literal>3 Aug 2020 25:00:00</literal> overflows to <literal>(Tue) 4 Aug
2020 01:00</literal>.
</simpara>
</listitem>
<listitem>
<simpara>
<literal>Mon</literal> gets applied, which advances the date to
<literal>Mon, 10 Aug 2020 01:00:00</literal>. The explanation of
relative keywords such as <literal>Mon</literal> is explained in the
section on <link linkend="datetime.formats.relative">relative
formats</link>.
</simpara>
</listitem>
</orderedlist>
</example>
<para>
In order to detect overflows in dates, you can use
<methodname>DateTimeImmutable::getLastErrors</methodname>, which will
include a warning if an overflow occured.
</para>
<example>
<title>Detecting overflown dates</title>
<programlisting role="php">
<![CDATA[
<?php
$d = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2021-17-35 16:60:97');
echo $d->format(DateTimeImmutable::RFC2822), "\n\n";
var_dump(DateTimeImmutable::GetLastErrors());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Sat, 04 Jun 2022 17:01:37 +0000
array(4) {
'warning_count' =>
int(2)
'warnings' =>
array(1) {
[19] =>
string(27) "The parsed date was invalid"
}
'error_count' =>
int(0)
'errors' =>
array(0) {
}
}
]]>
</screen>
</example>
<example>
<title>Greedy parsing behaviour</title>
<programlisting role="php">
<![CDATA[
<?php
print_r(date_parse_from_format('Gis', '60101'));
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Array
(
[year] =>
[month] =>
[day] =>
[hour] => 60
[minute] => 10
[second] => 0
[fraction] => 0
[warning_count] => 1
[warnings] => Array
(
[5] => The parsed time was invalid
)
[error_count] => 1
[errors] => Array
(
[4] => A two digit second could not be found
)
[is_localtime] =>
)
]]>
</screen>
<para>
The <literal>G</literal> 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 <literal>60</literal>.
</para>
<para>
The following <literal>i</literal> and <literal>s</literal> format
characters both require two digits. This means that <literal>10</literal>
is passed as minute (<literal>i</literal>), and that there are then not
enough digits left to parse for as second (<literal>s</literal>).
</para>
<para>
The <literal>errors</literal> array indicates this problem.
</para>
<para>
Additionally, an hour of <literal>60</literal> is outside the range
<literal>0</literal>-<literal>24</literal>, which causes the
<literal>warnings</literal> array to include a warning that the time is
invalid.
</para>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>DateTimeImmutable::__construct</function></member>
<member><function>DateTimeImmutable::getLastErrors</function></member>
<member><function>checkdate</function></member>
<member><function>strptime</function></member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|