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 character | Description | Example parsable values |
---|---|---|
Day | ||
d | Day of the month, 2 digits with leading zeros | 01 to 31 |
D | A textual representation of a day, three letters | Mon through Sun |
j | Day of the month without leading zeros | 1 to 31 |
l | A full textual representation of the day of the week | Sunday through Saturday |
S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd or th. |
z | The day of the year (starting from 0) | 0 through 365 |
Month | ||
F | A full textual representation of a month, such as January or March | January through December |
m | Numeric representation of a month, with leading zeros | 01 through 12 |
M | A short textual representation of a month, three letters | Jan through Dec |
n | Numeric representation of a month, without leading zeros | 1 through 12 |
Year | ||
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
y | A two digit representation of a year | Examples: 99 or 03 |
Time | ||
a | Lowercase Ante meridiem and Post meridiem | am or pm |
A | Uppercase Ante meridiem and Post meridiem | AM or PM |
g | 12-hour format of an hour without leading zeros | 1 through 12 |
G | 24-hour format of an hour without leading zeros | 0 through 23 |
h | 12-hour format of an hour with leading zeros | 01 through 12 |
H | 24-hour format of an hour with leading zeros | 00 through 23 |
i | Minutes with leading zeros | 00 to 59 |
s | Seconds with leading zeros | 00 through 59 |
u | Microseconds. | Example: 654321 |
v | Milliseconds. Same note applies as for u. | Example: 654 |
Timezone | ||
e | Timezone identifier | Examples: UTC, GMT, Atlantic/Azores |
O | Difference to Greenwich time (GMT) without colon between hours and minutes | Example: +0200 |
P | Difference to Greenwich time (GMT) with colon between hours and minutes | Example: +02:00 |
T | Timezone abbreviation, if known; otherwise the GMT offset. | Examples: EST, MDT, +05 |
Full Date/Time | ||
U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | See time() function. |
Whitespace and Separators | ||
(space) | One space or one tab | Example: |
# | One of the following separation symbol: ; : / . , - ( or ) | Example: / |
; : / . , - ( or ) | The specified character. | Example: - |
? | A random byte | Example: ^ (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 digit | Example: * 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 Epoch | Without !, 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 yet | Y-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 instead | Use 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