PHP.nl

date_parse_from_format

date_parse_from_format

Get info about given date formatted according to the specified format

array **date_parse_from_format** string $format string $datetime

Returns associative array with detailed info about given date/time.

format Documentation on how the is used, please refer to the documentation of . The same rules apply. format``DateTimeImmutable::createFromFormat

datetimeString representing the date/time.

Returns associative array with detailed info about given date/time.

The returned array has keys for , , , , , , , and . year``month``day``hour``minute``second``fraction``is_localtime

If is present then indicates the type of timezone. For type (UTC offset) the , fields are added; for type (abbreviation) the fields , are added; and for type (timezone identifier) the , are added. is_localtime``zone_type``1``zone``is_dst``2``tz_abbr``is_dst``3``tz_abbr``tz_id

The array includes and fields. The first one indicate how many warnings there were. The keys of elements array indicate the position in the given where the warning occurred, with the string value describing the warning itself. An example below shows such a warning. warning_count``warnings``warnings``datetime

The array also contains and fields. The first one indicate how many errors were found. The keys of elements array indicate the position in the given where the error occurred, with the string value describing the error itself. An example below shows such an error. error_count``errors``errors``datetime

Waarschuwing: > The number of array elements in the and arrays might be less than or if they occurred at the same position. warnings``errors``warning_count``error_count

This functions throws when the contains NULL-bytes. datetime

Voorbeeld: example

<?php
$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
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] =>
)

Voorbeeld: with warnings example

<?php
$date = "26 August 2022 22:30 pm";
$parsed = date_parse_from_format("j F Y G:i a", $date);

echo "Warnings count: ", $parsed['warning_count'], "\n";
foreach ($parsed['warnings'] as $position => $message) {
    echo "\tOn position {$position}: {$message}\n";
}
Warnings count: 1
	On position 23: The parsed time was invalid

Voorbeeld: with errors example

<?php
$date = "26 August 2022 CEST";
$parsed = date_parse_from_format("j F Y H:i", $date);

echo "Errors count: ", $parsed['error_count'], "\n";
foreach ($parsed['errors'] as $position => $message) {
    echo "\tOn position {$position}: {$message}\n";
}
Errors count: 3
	On position 15: A two digit hour could not be found
	On position 19: Not enough data available to satisfy format

DateTimeImmutable::createFromFormat``checkdate