In PHP, date_parse_from_format() reads a date string using a format you choose and returns its parts in an array.
It works like date_parse(), but you tell PHP exactly how the text is laid out, using the same format characters as date_create_from_format(). It also reports warnings and errors when the text does not match.
Syntax
Call date_parse_from_format() with the values you need. Start with this line.
Returns year 2024 and month 3, with day set to false.
This is a safe way to check strict user input such as 15/03/2024, because you decide the format. Parts that the format does not include are false in the result. Check error_count before trusting the numbers.
The format F Y means a full month name followed by a four-digit year.
PHP reads March as month 3 and 2024 as the year.
No day is in the text, so day is false.
var_export() with true turns false into printable text.
Frequently Asked Questions
It parses a date string using a format that you give and returns an array with the year, month, day, time parts, warnings, and errors.
date_create_from_format() returns a DateTime object (or false). date_parse_from_format() always returns an array that shows how the text was read, which is useful for validation.
The same ones as date_create_from_format(), such as d, j, m, n, Y, y, H, i, s, F, and M. For example, d/m/Y matches 15/03/2024.
A value is false when the format did not include that part. For instance, a format of F Y never sets the day.
Check that error_count is 0. When it is greater than 0, the errors array contains the messages and the positions where the text did not match.