PHP date_create_from_format Function: Syntax and Examples
If you need to create a PHP date object from a string with a non-standard format, you can use the date_create_from_format function. This function parses a date
The date_create_from_format() function parses a date string written in your format — not just the formats PHP guesses at — and returns a DateTime object. Use it whenever you receive a date in a fixed, non-standard layout (a CSV export, a third-party API, a user form) and need to read it back reliably.
This page covers the syntax, runnable examples, the timezone parameter, how to detect parse failures, and how the function differs from strtotime() and date_parse_from_format().
Syntax
date_create_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false$format— the layout of the input string, written with the same format characters as thedate()function. For example"Y-m-d H:i:s"matches"2023-03-02 17:30:45".$datetime— the date string to parse. Its layout must match$format.$timezone— (optional) aDateTimeZonefor the resulting object. If omitted, the current default timezone (set viadate_default_timezone_set()) is used. The parameter is ignored if$formatalready contains a timezone character such ase,O,P, orT.
It returns a DateTime object on success, or false if the string does not match the format.
date_create_from_format()is the procedural alias of theDateTime::createFromFormat()static method — they behave identically.
Examples
Parsing a standard date string
The function reads "2023-03-02 17:30:45" according to "Y-m-d H:i:s" and returns a DateTime object. Calling format() on it can re-render the date in any layout you like.
Re-formatting a non-standard date
Here the input uses a day, a three-letter month name, and a four-digit year (j-M-Y). The output is rendered in ISO order (Y-m-d) — proof that once a date is a DateTime object you are free to display it however you want.
Resetting the time with !
If your format has no time fields, the missing parts default to the current hour, minute, and second — which is rarely what you want for a date-only value. Prefix the format with ! to reset all unspecified fields to the Unix epoch (00:00:00):
<?php
$date = date_create_from_format('!Y-m-d', '2023-03-02');
echo $date->format('Y-m-d H:i:s'); // Output: 2023-03-02 00:00:00Supplying a timezone
<?php
$tz = new DateTimeZone('Europe/Paris');
$date = date_create_from_format('Y-m-d H:i:s', '2023-03-02 17:30:45', $tz);
echo $date->format('Y-m-d H:i:s P'); // Output: 2023-03-02 17:30:45 +01:00The third argument anchors the parsed date to Paris time, so the P token reports the +01:00 offset.
Handling parse failures
When the string does not match the format, the function returns false rather than throwing. Always check for it, and read DateTime::getLastErrors() for the details:
<?php
$date = date_create_from_format('Y-m-d', 'not-a-date');
if ($date === false) {
$errors = DateTime::getLastErrors();
echo "Parse failed: {$errors['error_count']} error(s)\n";
// Output: Parse failed: 3 error(s)
}Because false is falsy, a bare if (!$date) works too — but a strict === false check is safer when an empty value could legitimately mean something else.
Comparison with other date functions
| Function | Input | Returns |
|---|---|---|
date_create_from_format() | string + explicit format | DateTime (or false) |
strtotime() | string PHP guesses at | Unix timestamp int (or false) |
date_parse_from_format() | string + explicit format | associative array of components |
Reach for date_create_from_format() when the layout is fixed and you want a full DateTime object to manipulate. Use strtotime() for loose, human-style strings like "next Friday", and date_parse_from_format() when you only need the raw year/month/day numbers without building an object.
Conclusion
date_create_from_format() turns a string in an arbitrary, known layout into a DateTime object you can format, compare, and shift with the rest of PHP's date API. Remember to use the ! prefix for date-only values, pass a DateTimeZone when the source timezone matters, and always guard against the false return.