date_parse()

date_parse() — Returns associative array with detailed info about given date/time.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.2.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.

What it does

date_parse() parses the given datetime string according to the same rules as strtotime() and DateTimeImmutable::__construct(). Instead of returning a Unix timestamp (with strtotime()) or a DateTimeImmutable object (with DateTimeImmutable::__construct()), it returns an associative array with the information that it could detect in the given datetime string.

Signature

date_parse(string $datetime): array

Parameters

ParameterTypeWhat it takes
$datetimestringDate/time in format accepted by DateTimeImmutable::__construct().

Return value

Returns array with information about the parsed 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. If relative time elements are present in the datetime string such as +3 days, the returned array includes a nested array with the key relative. This array then contains the keys year, month, day, hour, minute, second, and if necessary weekday, and weekdays, depending on the ...

Example

A runnable snippet:

var_dump(date_parse("2006-12-12 10:00:00.5"));

Output:

array(12) { ["year"]=> int(2006) ["month"]=> int(12) ["day"]=> int(12) ["hour"]=> int(10) ["minute"]=> int(0) ["second"]=> int(0) ["fraction"]=> float(0.5) ["warning_count"]=> int(0) ["warnings"]=> array(0) { } ["error_count"]=> int(0) ["errors"]=> array(0) { } ["is_localtime"]=> bool(false) }

Changelog

VersionChange
7.2.0The 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

date_parse_from_format(), checkdate(), getdate()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page date_parse() (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.