Content · Programming · PHP

PHP DateTime::createFromFormat() Method: Usage and Examples

Learn how to parses text according to an explicit date pattern in PHP with syntax, practical examples, edge cases, and common mistakes.

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

When a PHP application needs to parses text according to an explicit date pattern, choosing the right API and checking its results helps keep code understandable and dependable. This guide explains DateTime::createFromFormat with focused examples and important details.

What Is DateTime::createFromFormat in PHP?

DateTime::createFromFormat parses text according to an explicit date pattern. Use the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().

Syntax:

DateTime::createFromFormat(format, datetime, timezone) : DateTime|false

Parameters and return value: Parameters: <required> string $format; <required> string $datetime; <optional> ?DateTimeZone $timezone = null. Check the return value or modified object rather than assuming the operation always succeeds.

Important: Use explicit IANA time zones in production and account for daylight-saving changes when calculating local schedules.

Example 1: Basic Usage

Start with a compact input so the main operation is visible.

<?php

$date = DateTime::createFromFormat("!d/m/Y", "15/06/2026");
echo $date->format("Y-m-d");

Output:

2026-06-15

Parses text according to an explicit date pattern. Use the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().

Example 2: A Different Input

The next example changes the input or option to illustrate another behavior.

<?php

$date = DateTime::createFromFormat("!Y-m-d H:i", "2026-09-21 17:45", new DateTimeZone("UTC"));
echo $date->format("c");

Output:

2026-09-21T17:45:00+00:00

Compare this result with the first example. Use the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().

Practical Example: Applying the Function

This example combines the API with a realistic processing step and shows where to check its result.

<?php

$sourceDates = ["10/02/2026", "11/03/2026"];
foreach ($sourceDates as $value) {
    $date = DateTime::createFromFormat("!d/m/Y", $value);
    $issues = DateTime::getLastErrors();
    if ($date === false || $issues !== false) { continue; }
    echo $date->format("Y-m-d") . PHP_EOL;
}

Output:

2026-02-10
2026-03-11

Adapt the inputs to your application and validate the output before storing or displaying it. Use the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().

Important Behavior and Edge Cases

  • Main purpose: Parses text according to an explicit date pattern.
  • Errors and boundaries: Use the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().
  • Environment: Use explicit IANA time zones in production and account for daylight-saving changes when calculating local schedules.

Comparison With Related Functions

Function or ApproachWhen to Use It
DateTime::createFromFormatParses text according to an explicit date pattern.
createFromFormatConsider this alternative when you need different behavior or a different abstraction.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming the operation succeeds for every inputUse the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().
Ignoring runtime requirements or variable outputUse explicit IANA time zones in production and account for daylight-saving changes when calculating local schedules.
Using the wrong API for the goalCompare DateTime::createFromFormat with createFromFormat before selecting an approach.

Key Takeaways

  • Parses text according to an explicit date pattern.
  • Use the ! modifier to reset unspecified fields; successful parsing may still produce warnings for invalid dates, so inspect getLastErrors().
  • Use explicit IANA time zones in production and account for daylight-saving changes when calculating local schedules.

Use DateTime::createFromFormat when you need to parses text according to an explicit date pattern. The three examples move from a simple call to a practical use. Validate all external input and handle failures explicitly.

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