On this page
DateTimeImmutable::createFromFormat
date_create_immutable_from_format
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
DateTimeImmutable::createFromFormat -- date_create_immutable_from_format — Parses a time string according to a specified format
Description
Object-oriented style
public static DateTimeImmutable::createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false
Procedural style
date_create_immutable_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false
Returns a new DateTimeImmutable object representing the date and time specified by the datetime string, which was formatted in the given format.
Parameters
format-
The format that the passed in string should be in. See the formatting options below. In most cases, the same letters as for the date() can be used.
All fields are initialised with the current date/time. In most cases you would want to reset these to "zero" (the Unix epoch,
1970-01-01 00:00:00 UTC). You do that by including the!character as first character in yourformat, or|as your last. Please see the documentation for each character below for more information.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
z(the day of the year), it is required that a year has already been parsed, for example through theYorycharacters.Letters that are used for parsing numbers allow a wide range of values, outside of what the logical range would be. For example, the
d(day of the month) accepts values in the range from00to99. 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.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
datetimestring for following format characters. An example on this page also illustrates this issue.The following characters are recognized in the formatparameter stringformatcharacterDescription Example parsable values Day --- --- dandjDay of the month, 2 digits with or without leading zeros 01to31or1to31. (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)DandlA textual representation of a day MonthroughSunorSundaythroughSaturday. 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 next date with the given day name. See the examples below for an explanation.SEnglish ordinal suffix for the day of the month, 2 characters. It's ignored while processing. st,nd,rdorth.zThe day of the year (starting from 0); must be preceded by Yory.0through365. (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)Month --- --- FandMA textual representation of a month, such as January or Sept JanuarythroughDecemberorJanthroughDecmandnNumeric representation of a month, with or without leading zeros 01through12or1through12. (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)Year --- --- XandxA full numeric representation of a year, up to 19 digits, optionally prefixed by +or-Examples: 0055,787,1999,-2003,+10191YA full numeric representation of a year, up to 4 digits Examples: 0055,787,1999,2003yA two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive) Examples: 99or03(which will be interpreted as1999and2003, respectively)Time --- --- aandAAnte meridiem and Post meridiem amorpmgandh12-hour format of an hour with or without leading zero 1through12or01through12(2 digit numbers higher than 12 are accepted, in which case they will make the day overflow. For example using14means02in the next AM/PM period)GandH24-hour format of an hour with or without leading zeros 0through23or00through23(2 digit numbers higher than 24 are accepted, in which case they will make the day overflow. For example using26means02:00the next day)iMinutes with leading zeros 00to59. (2 digit numbers higher than 59 are accepted, in which case they will make the hour overflow. For example using66means:06the next hour)sSeconds, with leading zeros 00through59(2 digit numbers higher than 59 are accepted, in which case they will make the minute overflow. For example using90means:30the next minute)vFraction in milliseconds (up to three digits) Example: 12(0.12seconds),345(0.345seconds)uFraction in microseconds (up to six digits) Example: 45(0.45seconds),654321(0.654321seconds)Timezone --- --- e,O,PandTTimezone identifier, or difference to UTC in hours, or difference to UTC with colon between hours and minutes, or timezone abbreviation Examples: UTC,GMT,Atlantic/Azoresor+0200or+02:00orEST,MDTFull Date/Time --- --- USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Example: 1292177455Whitespace and Separators --- --- (space) Zero or more spaces, tabs, NBSP (U+A0), or NNBSP (U+202F) characters Example: "\t"," "#One of the following separation symbol: ;,:,/,.,,,-,(or)Example: /;,:,/,.,,,-,(or)The specified character. Example: -?A random byte Example: ^(Be aware that for UTF-8 characters you might need more than one?. In this case, using*is probably what you want instead)*Random bytes until the next separator or digit Example: *inY-*-dwith the string2009-aWord-08will matchaWord!Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values ( 0for hour, minute, second and fraction,1for month and day,1970for year andUTCfor timezone information)Without !,all fields will be set to the current date and time.|Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values if they have not been parsed yet Y-m-d|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.+If this format specifier is present, trailing data in the string will not cause an error, but a warning instead Use DateTimeImmutable::getLastErrors() to find out whether trailing data was present. 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 DateTimeImmutable::getLastErrors().
To include literal characters in
format, you have to escape them with a backslash (\).If
formatdoes not contain the character!then portions of the generated date/time which are not specified informatwill be set to the current system time.If
formatcontains the character!, then portions of the generated date/time not provided informat, as well as values to the left-hand side of the!, will be set to corresponding values from the Unix epoch.If any time character is parsed, then all other time-related fields are set to "0", unless also parsed.
The Unix epoch is 1970-01-01 00:00:00 UTC.
datetime-
String representing the time.
timezone-
A DateTimeZone object representing the desired time zone.
If
timezoneis omitted ornullanddatetimecontains no timezone, the current timezone will be used.Note:
The
timezoneparameter and the current timezone are ignored when thedatetimeparameter either contains a UNIX timestamp (e.g.946684800) or specifies a timezone (e.g.2010-01-28T15:00:00+02:00).
Return Values
Returns a new DateTimeImmutable instance or false on failure.
Errors/Exceptions
This method throws ValueError when the datetime contains NULL-bytes.
Changelog
| Version | Description |
|---|---|
| 8.2.9 | The (space) specifier now also supports NBSP (U+A0) and NNBSP (U+202F) characters. |
| 8.2.0 | The X and x format specifiers have been added. |
| 8.0.21, 8.1.8, 8.2.0 | Now throws ValueError when NULL-bytes are passed into datetime, which previously was silently ignored. |
| 7.3.0 | The v format specifier has been added. |
Examples
Example #1 DateTimeImmutable::createFromFormat() example
Object-oriented style
<?php
$date = DateTimeImmutable::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
Example #2 Using predefined format constants with DateTimeImmutable::createFromFormat()
Object-oriented style
<?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');
?>
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()
<?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";
?>
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
<?php
echo DateTimeImmutable::createFromFormat('H\h i\m s\s','23h 15m 03s')->format('H:i:s');
?>
The above example will output something similar to:
23:15:03
Example #5 Overflow behaviour
<?php
echo DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2021-17-35 16:60:97')->format(DateTimeImmutable::RFC2822);
?>
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:
-
97seconds overflows to1minute, leaving37seconds. -
61minutes overflows to1hour, leaving1minutes. -
35days overflows to1month, leaving4days. The amount of days that are left over depends on the month, as not every month has the same amount of days. -
18months overflows to1year, leaving6months.
Example #6 Overflowing day name behaviour
<?php
$d = DateTime::createFromFormat(DateTimeInterface::RFC1123, 'Mon, 3 Aug 2020 25:00:00 +0000');
echo $d->format(DateTime::RFC1123), "\n";
?>
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:
-
3 Aug 2020 25:00:00overflows to(Tue) 4 Aug 2020 01:00. -
Mongets applied, which advances the date toMon, 10 Aug 2020 01:00:00. The explanation of relative keywords such asMonis explained in the section on relative formats.
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
<?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());
?>
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
<?php
print_r(date_parse_from_format('Gis', '60101'));
?>
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 G 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 60.
The following i and s format characters both require two digits. This means that 10 is passed as minute (i), and that there are then not enough digits left to parse for as second (s).
The errors array indicates this problem.
Additionally, an hour of 60 is outside the range 0-24, which causes the warnings array to include a warning that the time is invalid.
See Also
- DateTimeImmutable::__construct() - Returns new DateTimeImmutable object
- DateTimeImmutable::getLastErrors() - Returns the warnings and errors
- checkdate() - Validate a Gregorian date
- strptime() - Parse a time/date generated with strftime
© 1997–2023 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/datetimeimmutable.createfromformat.php