PHP Function Reference

PHP date_parse_from_format() Function



The PHP date_parse_from_format() function returns an associative array with detailed info about given date/time formatted according to the specified format.

Syntax

date_parse_from_format(format, datetime)

Parameters

format Required. Specify the format string to use. Refer to the table below for formatting options.
datetime Required. Specify a date/time string.

format parameter string

Format characterDescriptionExample parsable values
Day
dDay of the month, 2 digits with leading zeros01 to 31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
lA full textual representation of the day of the weekSunday through Saturday
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th.
zThe day of the year (starting from 0)0 through 365
Month
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
Year
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
Time
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds with leading zeros00 through 59
uMicroseconds.Example: 654321
vMilliseconds. Same note applies as for u.Example: 654
Timezone
eTimezone identifierExamples: UTC, GMT, Atlantic/Azores
ODifference to Greenwich time (GMT) without colon between hours and minutesExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutesExample: +02:00
TTimezone abbreviation, if known; otherwise the GMT offset.Examples: EST, MDT, +05
Full Date/Time
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)See time() function.
Whitespace and Separators
(space)One space or one tabExample:
#One of the following separation symbol: ; : / . , - ( or )Example: /
; : / . , - ( or ) The specified character.Example: -
?A random byteExample: ^ (Be aware that for UTF-8 characters you might need more than one ?. In this case, using * is probably what you want instead)
*Random bytes until the next separator or digitExample: * in Y-*-d with the string 2009-aWord-08 will match aWord
!Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix EpochWithout !, all fields will be set to the current date and time.
|Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch if they have not been parsed yetY-m-d| will set the year, month and day to the information found in the string to parse, and sets the hour, minute and second to 0.
+If this format specifier is present, trailing data in the string will not cause an error, but a warning insteadUse DateTime::getLastErrors() to find out whether trailing data was present.

Unrecognized characters in the format string will cause the parsing to fail and an error message is appended to the returned structure.


Return Value

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

Example: date_parse_from_format() example

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

<?php
//datetime string
$date = "15.8.2015 10:30+05:30";
  
//getting the details about the date
print_r(date_parse_from_format("j.n.Y H:iP", $date));
?>

The output of the above code will be:

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

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

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => 19800
    [is_dst] => 
)

Example: another format

Consider one more example where a different format is used with datetime string.

<?php
//datetime string
$date = "15-Dec-2015 10:15:28";
  
//getting the details about the date
print_r(date_parse_from_format("d-M-Y H:i:s", $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] => 
)

❮ PHP Date and Time Reference