In PHP, date_parse() reads a date and time string and returns its parts (year, month, day, hour, minute, second) in an array.
Unlike date_create(), it does not build a date object. It only shows what PHP understood from the text, along with any warnings and errors, so it is handy for checking input.
Syntax
Call date_parse() with the values you need. Start with this line.
PHP
$parts = date_parse("2024-03-15 14:30:45");
Call
What it does
date_parse("2024-03-15 14:30:45")
Returns an array with year 2024, month 3, day 15, hour 14, minute 30, and second 45.
date_parse("10:30")
Returns hour 10 and minute 30, with year, month, and day set to false.
date_parse("2024-02-30")
Returns the parts plus one warning, because the date does not exist.
Any part PHP did not find in the text is false. The array also has warning_count, warnings, error_count, errors, and is_localtime. date_parse() only reads the text and does not check that the date is real. Use checkdate() for that.
Library declarations
PHP
date_parse(string $datetime): array
Parameters
Parameter
Description
datetime
Required. The date and time string to parse, in a format PHP understands.
Return Value
Return Value
Description
array
An array with year, month, day, hour, minute, second, fraction, warning_count, warnings, error_count, errors, and is_localtime. Extra keys appear for timezones and relative text.
PHP Examples
Example 1 — Parse a Full Date and Time
Pass a date and time string and read each part from the returned array.
Errors: 0
Warnings: 1
The parsed date was invalid
Not a real date
How It Works
The text has a clear year, month, and day, so there are no errors.
PHP adds a warning because February 30 does not exist.
reset() returns the first warning message from the warnings array.
checkdate() confirms that February 30, 2024 is not a real date.
Frequently Asked Questions
It reads a date and time string and returns an array with its year, month, day, hour, minute, and second, plus any warnings and errors. It does not create a date object.
date_create() builds a usable DateTime object, while date_parse() only shows how PHP understood the text. Use date_parse() when you want to inspect or validate input.
A value is false when the text does not contain that part. For example, parsing only a time leaves year, month, and day as false.
No. A date like 2024-02-30 only adds a warning. Combine it with checkdate() if you need to be sure the date really exists.
date_parse() works out the format on its own. date_parse_from_format() lets you give an exact format, such as d/m/Y, which is better for strict input.