Content · Programming · PHP

PHP date_create_from_format() Function: Usage and Examples

Learn how to parse a date using a specified pattern in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to parse a date using a specified pattern. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

date_create_from_format: The exclamation mark in a format initializes otherwise unspecified fields instead of taking them from now.

What Is date_create_from_format in PHP?

Parse a date using a specified pattern. The exclamation mark in a format initializes otherwise unspecified fields instead of taking them from now.

Syntax:

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

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: A non-false DateTime can still be accompanied by warnings for normalized invalid calendar dates.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

$date = date_create_from_format("!d/m/Y", "16/10/2026", new DateTimeZone("UTC"));
echo $date->format("Y-m-d");

Output:

2026-10-16

The code focuses on the main operation. The exclamation mark in a format initializes otherwise unspecified fields instead of taking them from now.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

$date = date_create_from_format("!Y-m-d", "2026-02-30", new DateTimeZone("UTC"));
var_dump(DateTime::getLastErrors());

Output:

array(4) {
  ["warning_count"]=>
  int(1)
  ["warnings"]=>
  array(1) {
    [10]=>
    string(27) "The parsed date was invalid"
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
}

What changes? A non-false DateTime can still be accompanied by warnings for normalized invalid calendar dates.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$input = "16/10/2026";
$date = date_create_from_format("!d/m/Y", $input, new DateTimeZone("UTC"));
$errors = date_get_last_errors();
if ($date === false || ($errors !== false && ($errors["warning_count"] > 0 || $errors["error_count"] > 0))) {
    throw new InvalidArgumentException("Invalid date");
}
echo $date->format("Y-m-d");

Output:

2026-10-16

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The exclamation mark in a format initializes otherwise unspecified fields instead of taking them from now.

Important Behavior and Edge Cases

  • Core behavior: The exclamation mark in a format initializes otherwise unspecified fields instead of taking them from now.
  • Inputs and results: A non-false DateTime can still be accompanied by warnings for normalized invalid calendar dates.
  • Alternatives: date_create_immutable_from_format() provides the same parsing behavior with immutability.

Comparison With Related Functions

Function or ApproachWhen to Use It
date_create_from_formatParse a date using a specified pattern.
Related PHP approachdate_create_immutable_from_format() provides the same parsing behavior with immutability.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionA non-false DateTime can still be accompanied by warnings for normalized invalid calendar dates.
Confusing this API with a related operationdate_create_immutable_from_format() provides the same parsing behavior with immutability.
Assuming output is identical across environmentsCheck return values and required extensions or versions. Use explicit locale, timezone or encoding settings when they affect the result.

Key Takeaways

  • date_create_from_format helps you parse a date using a specified pattern.
  • The exclamation mark in a format initializes otherwise unspecified fields instead of taking them from now.
  • A non-false DateTime can still be accompanied by warnings for normalized invalid calendar dates.

Use date_create_from_format when you need to parse a date using a specified pattern. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.