PHP | date_parse_from_format() Function

PHP date_parse_from_format() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP date_parse_from_format() Function.

PHP date_parse_from_format() function accepts a format string and a date string as parameters and, returns information about the given date in the specified format.

Syntax

Following below is the syntax to use this function -

date_parse($date)


Parameter Details

Sr.NoParameter & Description
1

format(Mandatory)

This is a string value representing the format in which you need to format the info about the date.

2

date(Mandatory)

This is a string value representing the date for which you need the information about.


Return Value

This PHP function returns an array containing the information about the given date in the specified format.

PHP Version

This function was first introduced as part of the core PHP v 5.3.0 and, it works with all of the later versions.

Example1

Below example shows the usage of the date_parse_from_format() function -

<?php
   //Creating a DateTime object
   $date = "25-Mar-1989";
   $format = "d-M-Y";
   $res = date_parse_from_format($format, $date);
   print_r($res);
?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [year] => 1989
    [month] => 3
    [day] => 25
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

Example2

Let us see the different formats to parse a date -

<?php
   $res1 = date_parse_from_format("j.n.Y", "25.8.2014");
   print_r($res1);
   
   $res2 = date_parse_from_format("y-d-m", "2014-25-8");
   print_r($res2);
   
   $res3 = date_parse_from_format("n/j/y", "8/25/2014");
   print_r($res3);
   
   $res4 = date_parse_from_format("D.M.Y", "25.8.2014");
   print_r($res4);
   
   $res5 = date_parse_from_format("H/i/s", "12/32/25");
   print_r($res5);
?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [year] => 2014
    [month] => 8
    [day] => 25
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)
Array
(
    [year] => 2020
    [month] => 25
    [day] => 14
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 1
    [warnings] => Array
        (
            [7] => The parsed date was invalid
        )

    [error_count] => 2
    [errors] => Array
        (
            [2] => The separation symbol could not be found
            [7] => Trailing data
        )

    [is_localtime] =>
)
Array
(
    [year] => 2020
    [month] => 8
    [day] => 25
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 1
    [errors] => Array
        (
            [7] => Trailing data
        )

    [is_localtime] =>
)
Array
(
    [year] => 8
    [month] =>
    [day] =>
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 4
    [errors] => Array
        (
            [0] => A textual day could not be found
            [3] => The separation symbol could not be found
            [4] => Trailing data
        )

    [is_localtime] =>
)
Array
(
    [year] =>
    [month] =>
    [day] =>
    [hour] => 12
    [minute] => 32
    [second] => 25
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

Example3

Below example shows the usage of the date_parse_from_format() function with relative formats -

<?php
   print_r(date_parse_from_format("Y-m-d", "2009-18-18-+52 week +25 hour"));
   print("\n");
   print_r(date_parse_from_format("Y-m-d", "1990-06-06 +52 week +25 hour"));
?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [year] => 2009
    [month] => 18
    [day] => 18
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 1
    [warnings] => Array
        (
            [10] => The parsed date was invalid
        )

    [error_count] => 1
    [errors] => Array
        (
            [10] => Trailing data
        )

    [is_localtime] =>
)

Array
(
    [year] => 1990
    [month] => 6
    [day] => 6
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 1
    [errors] => Array
        (
            [10] => Trailing data
        )

    [is_localtime] =>
)


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_sub() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.