Date Analysis Using date_parse() and date_parse_from_format() Functions

mainick

Maico Orazio

Posted on October 9, 2023

Date Analysis Using date_parse() and date_parse_from_format() Functions

The date_parse() function is used to obtain detailed information about a specific date.

This function returns an associative array containing more details about the parsed date, or it returns false in case of an error. It accepts a single argument specifying the date in a format accepted by the strtotime() function.

$date_parsed = date_parse('2023-06-22T07:30:09+01:00');
print_r($date_parsed);
Enter fullscreen mode Exit fullscreen mode

The example given above returns the following information:

Array
(
    [year] => 2023
    [month] => 6
    [day] => 22
    [hour] => 7
    [minute] => 30
    [second] => 9
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => 3600
    [is_dst] => 
)
Enter fullscreen mode Exit fullscreen mode

We can perform date analysis starting from a specific format.

The date_parse_from_format() function is used to obtain detailed information about the specified date based on the specified format.

The date_parse_from_format() function accepts, as its first argument, a string representing the format and then the date. It returns, like date_parse(), an associative array of detailed information about the date in the specified format, or it returns false in case of an error.

$date_parsed = date_parse_from_format('d/m/Y H:i:s T', '22/06/2023 07:30:09 Europe/Rome');
print_r($date_parsed);
Enter fullscreen mode Exit fullscreen mode

The example given above returns the following information:

Array
(
    [year] => 2023
    [month] => 6
    [day] => 22
    [hour] => 7
    [minute] => 30
    [second] => 9
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 1
    [zone_type] => 3
    [tz_id] => Europe/Rome
)
Enter fullscreen mode Exit fullscreen mode

If we try to specify an incorrect format, we get an incorrect result where the related errors are also listed in errors.

$date_parsed = date_parse_from_format('Y-m-d H:i:s T', '22/06/2023 07:30:09 Europe/Rome');
print_r($date_parsed);
Enter fullscreen mode Exit fullscreen mode
Array
(
    [year] => 22
    [month] => 6
    [day] => 20
    [hour] => 23
    [minute] => 7
    [second] => 30
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 8
    [errors] => Array
        (
            [2] => Unexpected data found.
            [5] => Unexpected data found.
            [10] => Unexpected data found.
            [16] => Trailing data
        )

    [is_localtime] => 1
    [zone_type] => 0
)
Enter fullscreen mode Exit fullscreen mode

Good work 👨‍💻

đź’– đź’Ş đź™… đźš©
mainick
Maico Orazio

Posted on October 9, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related