PHP date_create_from_format() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, date_create_from_format() builds a DateTime object from a date string that follows a format you describe. It returns false if the string does not match that format.

Unlike date_create(), which guesses the format, this function reads the string exactly as you say. That makes it ideal for dates like 15/01/2024 where the day and month order would otherwise be unclear. It is the procedural alias of DateTime::createFromFormat().

Syntax

Call date_create_from_format() with the values you need. Start with this line.

PHP
$date = date_create_from_format('d/m/Y', '15/01/2024');
CallWhat it does
date_create_from_format('d/m/Y', '15/01/2024')Reads the string as day, month and year, giving January 15, 2024.
date_create_from_format('!d/m/Y', '15/01/2024')The ! resets all other fields, so the time becomes 00:00:00.
date_create_from_format('Y-m-d H:i', '2024-01-15 10:30', $tz)Reads the date and time using a DateTimeZone object.

This function is an alias of DateTime::createFromFormat(). Any part of the date that your format does not include is filled in with the current time, so results can change from run to run. Start the format with ! or end it with | to reset the missing fields to the Unix epoch values instead.

Library declarations
PHP
date_create_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false

Parameters

ParameterDescription
formatRequired. The format of the input string. It uses the same letters as date(), for example d, m, Y, H, i and s. Add ! at the start or | at the end to reset unparsed fields.
datetimeRequired. The date and time string that matches the format exactly.
timezoneOptional. A DateTimeZone object. It is ignored if the string contains a timezone or if the format uses U (Unix timestamp). If null, the default timezone is used.

Return Value

Return ValueDescription
DateTimeA new DateTime object when the string matched the format.
falseReturned when the string does not match the format.

PHP Examples

Example 1 — Reading Custom Date Formats

Describe the layout of each string with format letters. PHP then reads the parts in the right order.

PHP
<?php
$d1 = date_create_from_format('d/m/Y H:i', '15/01/2024 10:30');
echo date_format($d1, 'Y-m-d H:i'), PHP_EOL;

$d2 = date_create_from_format('j-M-Y', '5-Mar-2024');
echo date_format($d2, 'Y-m-d'), PHP_EOL;

$d3 = date_create_from_format('Y-m-d H:i:s', '2024-01-15 10:30:00');
echo date_format($d3, 'l jS F'), PHP_EOL;

How It Works

  1. The format d/m/Y H:i tells PHP that the string is day, month, year, then hour and minute.
  2. j-M-Y reads a day without a leading zero, a short month name such as Mar, and a year.
  3. Every DateTime created this way can be printed again with date_format().
  4. The last call prints the weekday and the day with the ordinal suffix th.

Example 2 — Resetting Fields and Format Mismatch

The ! character resets every field that is not in your format. A string that does not match the format gives false.

PHP
<?php
$d = date_create_from_format('!d/m/Y', '15/01/2024');
echo date_format($d, 'Y-m-d H:i:s'), PHP_EOL;

$bad = date_create_from_format('Y-m-d', '15/01/2024');
var_dump($bad);

$year = date_create_from_format('!Y', '2024');
echo date_format($year, 'Y-m-d'), PHP_EOL;

How It Works

  1. The ! at the start sets all unparsed fields to zero, so the time is exactly midnight.
  2. The second string uses slashes but the format expects dashes, so the match fails and false is returned.
  3. With only a year in the format, month and day come from the epoch defaults of 01 and 01.
  4. That is why '2024' becomes 2024-01-01.

Example 3 — Timezones and Unix Timestamps

Pass a DateTimeZone for local text, or use the U format letter to read a Unix timestamp.

PHP
<?php
$tz = new DateTimeZone('America/New_York');
$d = date_create_from_format('Y-m-d H:i', '2024-01-15 09:00', $tz);
echo date_format($d, 'c'), PHP_EOL;

$u = date_create_from_format('U', '1705314600');
echo date_format($u, 'Y-m-d H:i:s P'), PHP_EOL;

How It Works

  1. The string has no timezone, so the America/New_York timezone is attached to the new object.
  2. The c format prints an ISO 8601 date, showing the -05:00 offset used in January.
  3. The U format letter reads a Unix timestamp, here 1705314600.
  4. A timestamp always sets the timezone to +00:00, so the clock shows 10:30:00.

Frequently Asked Questions

It creates a DateTime from a string using a format that you provide. It is useful when you receive dates in a fixed layout such as 15/01/2024 and need to read them correctly.
Yes. date_create_from_format() is the procedural alias of the static method DateTime::createFromFormat(). Both return a DateTime object or false.
Fields that are not in your format are filled with the current date and time. Add ! at the start of the format, or | at the end, to reset them to the epoch values and get a stable result.
The function returns false. You can also call date_get_last_errors() to read warnings and errors about the parsing. Always compare the result with false before using it.
Use date_create_immutable_from_format() or DateTimeImmutable::createFromFormat(). They read dates the same way, but return a DateTimeImmutable that cannot be changed in place.

Next: date_create_immutable()

date_create_immutable() builds a new DateTimeImmutable object from a date and time string.

date_create_immutable() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful