date_parse_from_format()
date_parse_from_format() — Get info about given date formatted according to the specified format.
Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.3.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
date_parse_from_format(string $format, string $datetime): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $format | string | Documentation on how the format is used, please refer to the documentation of DateTimeImmutable::createFromFormat(). The same rules apply. |
| $datetime | string | String representing the date/time. |
Return value
Returns associative array with detailed info about given date/time. The returned array has keys for year, month, day, hour, minute, second, fraction, and is_localtime. If is_localtime is present then zone_type indicates the type of timezone. For type 1 (UTC offset) the zone, is_dst fields are added; for type 2 (abbreviation) the fields tz_abbr, is_dst are added; and for type 3 (timezone identifier) the tz_abbr, tz_id are added. The array includes warning_count and warnings fields. The first one indicate how many warnings there were. The keys of elements warnings array indicate the position in the given datetime where the warning occurred, with the string value describing the warning itself. ...
Example
A runnable snippet:
$date = "6.1.2009 13:00+01:00"; print_r(date_parse_from_format("j.n.Y H:iP", $date));
Output:
Array ( [year] => 2009 [month] => 1 [day] => 6 [hour] => 13 [minute] => 0 [second] => 0 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => 1 [zone_type] => 1 [zone] => 3600 [is_dst] => )
Changelog
| Version | Change |
|---|---|
| 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.2.0 | The zone element of the returned array represents seconds instead of minutes now, and its sign is inverted. For instance -120 is now 7200. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page date_parse_from_format() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.