PHP date_parse_from_format() Function

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

Definition

In PHP, date_parse_from_format() reads a date string using a format you choose and returns its parts in an array.

It works like date_parse(), but you tell PHP exactly how the text is laid out, using the same format characters as date_create_from_format(). It also reports warnings and errors when the text does not match.

Syntax

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

PHP
$parts = date_parse_from_format("d/m/Y H:i", "15/03/2024 09:05");
CallWhat it does
date_parse_from_format("d/m/Y H:i", "15/03/2024 09:05")Returns year 2024, month 3, day 15, hour 9, and minute 5.
date_parse_from_format("Y-m-d", "2024-03-15 extra")Returns error_count 1 because of trailing data.
date_parse_from_format("F Y", "March 2024")Returns year 2024 and month 3, with day set to false.

This is a safe way to check strict user input such as 15/03/2024, because you decide the format. Parts that the format does not include are false in the result. Check error_count before trusting the numbers.

Library declarations
PHP
date_parse_from_format(string $format, string $datetime): array

Parameters

ParameterDescription
formatRequired. The format of the text, using characters such as d, m, Y, H, and i.
datetimeRequired. The date and time text to parse.

Return Value

Return ValueDescription
arrayAn array with year, month, day, hour, minute, second, fraction, warning_count, warnings, error_count, errors, and is_localtime.

PHP Examples

Example 1 — Parse a Day/Month/Year String

Tell PHP the text uses day, month, year, and a 24-hour time.

PHP
<?php
$parts = date_parse_from_format("d/m/Y H:i", "15/03/2024 09:05");

echo "Date: ", $parts["year"], "-", $parts["month"], "-", $parts["day"], PHP_EOL;
echo "Time: ", sprintf("%02d:%02d", $parts["hour"], $parts["minute"]), PHP_EOL;
echo "Seconds: ", $parts["second"], PHP_EOL;

How It Works

  1. The format d/m/Y H:i matches the text 15/03/2024 09:05.
  2. PHP returns the year, month, and day as plain integers.
  3. sprintf() pads the hour and minute to two digits.
  4. The format has no seconds, so the second is 0.

Example 2 — Detect Text That Does Not Match

Extra text after the date creates an error. Compare it to a clean match.

PHP
<?php
$bad = date_parse_from_format("Y-m-d", "2024-03-15 extra");
echo "Errors: ", $bad["error_count"], PHP_EOL;
echo reset($bad["errors"]), PHP_EOL;

$ok = date_parse_from_format("Y-m-d", "2024-03-15");
echo "Errors: ", $ok["error_count"], PHP_EOL;

How It Works

  1. The format Y-m-d covers only the date, but the text has extra words.
  2. PHP reports one error with the message Trailing data.
  3. reset() returns the first message from the errors array.
  4. The clean text 2024-03-15 matches the format, so there are no errors.

Example 3 — Month and Year Only

Parts missing from the format are returned as false.

PHP
<?php
$parts = date_parse_from_format("F Y", "March 2024");

echo "Year: ", $parts["year"], PHP_EOL;
echo "Month: ", $parts["month"], PHP_EOL;
echo "Day: ", var_export($parts["day"], true), PHP_EOL;

How It Works

  1. The format F Y means a full month name followed by a four-digit year.
  2. PHP reads March as month 3 and 2024 as the year.
  3. No day is in the text, so day is false.
  4. var_export() with true turns false into printable text.

Frequently Asked Questions

It parses a date string using a format that you give and returns an array with the year, month, day, time parts, warnings, and errors.
date_create_from_format() returns a DateTime object (or false). date_parse_from_format() always returns an array that shows how the text was read, which is useful for validation.
The same ones as date_create_from_format(), such as d, j, m, n, Y, y, H, i, s, F, and M. For example, d/m/Y matches 15/03/2024.
A value is false when the format did not include that part. For instance, a format of F Y never sets the day.
Check that error_count is 0. When it is greater than 0, the errors array contains the messages and the positions where the text did not match.

Next: date_sub()

date_sub() subtracts a DateInterval (such as 10 days or 2 hours) from a DateTime object.

date_sub() 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