This section describes all the different formats in a BNF-like format, that the
DateTimeImmutable, DateTime,
date_create_immutable(),
date_create(), date_parse(), and
strtotime() parser understands. The formats are grouped
by section. In most cases formats from different sections, separated by
whitespace, comma or dot, can be used in the same date/time string. For each
of the supported formats, one or more examples are given, as well
as a description for the format. Characters in single quotes in
the formats are case-insensitive ('t' could
be t or T), characters in
double quotes are case-sensitive ("T" is only
T).
To format DateTimeImmutable and DateTime objects, please refer to the documentation of the DateTimeInterface::format() method.
A general set of rules should be taken into account.
false if any number is outside of
the ranges, and DateTimeImmutable::__construct() throws
an exception.
There is an additional check if an invalid date is provided:
<?php
$res = date_parse("2015-09-31");
var_dump($res["warnings"]);
?>
以上例程会输出:
array(1) {
[11] =>
string(27) "The parsed date was invalid"
}
It is already possible to handle the edge cases, but then DateTimeImmutable::createFromFormat() must be used while supplying the correct format.
<?php
$res = DateTimeImmutable::createFromFormat("Y-m-d", "2015-09-34");
var_dump($res);
以上例程会输出:
object(DateTimeImmutable)#1 (3) {
["date"]=>
string(26) "2015-10-04 17:24:43.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/London"
}