PHP | date_parse() Function

PHP date_parse() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_parse() Function.

The PHP date_parse() function accepts a date as a parameter, parses it and, returns information about the given date in form of an array.

Syntax

Following below is the syntax to use this function -

date_parse($date)


Parameter Details

Sr.NoParameter & Description
1

date(Mandatory)

This is a date string (should be accepted by the strtotime()) for which you need the info about.


Return Value

It returns an array, containing information about the given date. Incase of failure, this function returns FALSE.

PHP Version

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

Example1

Following example illustrates the usage of the PHP date_parse() function -

<?php
   print_r(date_parse("2009-11-09 07:30:25.5")); 
?>

Output

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

Array
(
    [year] => 2009
    [month] => 11
    [day] => 9
    [hour] => 7
    [minute] => 30
    [second] => 25
    [fraction] => 0.5
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

Example2

The following example below prints an information about the different dates -

<?php
   $date1 = date_parse("25-09-1989");  
   print_r($date1); 
   print("\n");
   $date2 = date_parse("14-02-2012");  
   print_r($date2); 
   print("\n");
   $date3 = date_parse("11-19-2005");  
   print_r($date3); 
   print("\n");
   $date4 = date_parse("17-07-2020");  
   print_r($date4); 
   print("\n");
   $date5 = date_parse("07-11-1995");  
   print_r($date5); 
   print("\n");
?>

Output

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

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

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

    [is_localtime] =>
)

Array
(
    [year] => 2012
    [month] => 2
    [day] => 14
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

Array
(
    [year] =>
    [month] =>
    [day] =>
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 1
    [warnings] => Array
        (
            [5] => Double timezone specification
        )

    [error_count] => 2
    [errors] => Array
        (
            [0] => Unexpected character
            [1] => Unexpected character
        )

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => -68400
    [is_dst] =>
)

Array
(
    [year] => 2020
    [month] => 7
    [day] => 17
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

Array
(
    [year] => 1995
    [month] => 11
    [day] => 7
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

Example3

The following example, shows the usage of the date_parse() with relative formats -

<?php
   print_r(date_parse("2009-18-18"));
   print("\n");
   print_r(date_parse("1990-06-06 +52 week +25 hour"));
?>

Output

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

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

    [error_count] => 1
    [errors] => Array
        (
            [6] => Unexpected character
        )

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => -64800
    [is_dst] =>
)

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

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

    [is_localtime] =>
    [relative] => Array
        (
            [year] => 0
            [month] => 0
            [day] => 364
            [hour] => 25
            [minute] => 0
            [second] => 0
        )

)


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

Do feel free to ask your questions where necessary and we 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.