PHP Function Reference

PHP date_parse() Function



The PHP date_parse() function returns an associative array with detailed info about given date/time.

Syntax

date_parse(datetime)

Parameters

datetime Required. Specify a date/time string. It should be in valid Date and Time Formats.

Return Value

Returns array with information about the parsed date/time on success or false on failure.

Exceptions

In case the date/time format has an error, the element 'errors' will contains the error messages.

Example: date_parse() example

The example below shows the usage of date_parse() function.

<?php
//datetime string
$date = "15-Dec-2015 10:15:28";
  
//getting the details about the date
print_r(date_parse($date));
?>

The output of the above code will be:

Array
(
    [year] => 2015
    [month] => 12
    [day] => 15
    [hour] => 10
    [minute] => 15
    [second] => 28
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] => 
)

Example: datetime string with relative formats

Consider one more example where the datetime string contains relative formats.

<?php
//datetime string
$date = "15-Nov-2015 10:15:28 +1 week +1 hour";
  
//getting the details about the date
print_r(date_parse($date));
?>

The output of the above code will be:

Array
(
    [year] => 2015
    [month] => 11
    [day] => 15
    [hour] => 10
    [minute] => 15
    [second] => 28
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

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

)

❮ PHP Date and Time Reference