|
DateTimeImmutable::createFromFormatdate_create_immutable_from_formatParses a time string according to a specified format DescriptionObject-oriented style
public static DateTimeImmutablefalse DateTimeImmutable::createFromFormat(string
$format, string $datetime, DateTimeZonenull $timezone = null)Procedural style DateTimeImmutablefalse date_create_immutable_from_format(string
$format, string $datetime, DateTimeZonenull $timezone = null)
Returns a new DateTimeImmutable object representing the date and time specified by the
Parameters
Return Values
Returns a new DateTimeImmutable instance or Errors/Exceptions
This method throws ValueError when the
Changelog
ExamplesExample #1 DateTimeImmutable::createFromFormat example Object-oriented style Example #2 Using predefined format constants with DateTimeImmutable::createFromFormat Object-oriented style The formatting constants as used in this example consist of a string of characters for formatting a DateTimeImmutable object. In most cases, these letters match with the same elements of date/time information as the ones defined in the parameters section above, but they tend to be more lenient. Example #3 Intricacies of DateTimeImmutable::createFromFormat The above example will output something similar to: 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 Example #4 Format string with literal characters The above example will output something similar to: 23:15:03 Example #5 Overflow behaviour The above example will output something similar to: Sat, 04 Jun 2022 17:01:37 +0000 Although the result looks odd, it is correct, as the following overflows happen:
Example #6 Overflowing day name behaviour The above example will output something similar to: Mon, 10 Aug 2020 01:00:00 +0000 Although the result looks odd, it is correct, as the following overflows happen:
In order to detect overflows in dates, you can use DateTimeImmutable::getLastErrors, which will include a warning if an overflow occured. Example #7 Detecting overflown dates The above example will output something similar to:
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) {
}
}
Example #8 Greedy parsing behaviour The above example will output something similar to:
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] =>
)
The
The following
The
Additionally, an hour of See Also
|