W3docs

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 the date() 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) a DateTimeZone for the resulting object. If omitted, the current default timezone (set via date_default_timezone_set()) is used. The parameter is ignored if $format already contains a timezone character such as e, O, P, or T.

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 the DateTime::createFromFormat() static method — they behave identically.

Examples

Parsing a standard date string

php— editable, runs on the server

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

php— editable, runs on the server

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:00

Supplying 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:00

The 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

FunctionInputReturns
date_create_from_format()string + explicit formatDateTime (or false)
strtotime()string PHP guesses atUnix timestamp int (or false)
date_parse_from_format()string + explicit formatassociative 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.

Practice

Practice
What does the date_create_from_format function in PHP do?
What does the date_create_from_format function in PHP do?
Was this page helpful?