PHP date_parse() Function

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

Definition

In PHP, date_parse() reads a date and time string and returns its parts (year, month, day, hour, minute, second) in an array.

Unlike date_create(), it does not build a date object. It only shows what PHP understood from the text, along with any warnings and errors, so it is handy for checking input.

Syntax

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

PHP
$parts = date_parse("2024-03-15 14:30:45");
CallWhat it does
date_parse("2024-03-15 14:30:45")Returns an array with year 2024, month 3, day 15, hour 14, minute 30, and second 45.
date_parse("10:30")Returns hour 10 and minute 30, with year, month, and day set to false.
date_parse("2024-02-30")Returns the parts plus one warning, because the date does not exist.

Any part PHP did not find in the text is false. The array also has warning_count, warnings, error_count, errors, and is_localtime. date_parse() only reads the text and does not check that the date is real. Use checkdate() for that.

Library declarations
PHP
date_parse(string $datetime): array

Parameters

ParameterDescription
datetimeRequired. The date and time string to parse, in a format PHP understands.

Return Value

Return ValueDescription
arrayAn array with year, month, day, hour, minute, second, fraction, warning_count, warnings, error_count, errors, and is_localtime. Extra keys appear for timezones and relative text.

PHP Examples

Example 1 — Parse a Full Date and Time

Pass a date and time string and read each part from the returned array.

PHP
<?php
$parts = date_parse("2024-03-15 14:30:45");

echo "Year: ", $parts["year"], PHP_EOL;
echo "Month: ", $parts["month"], PHP_EOL;
echo "Day: ", $parts["day"], PHP_EOL;
echo "Time: ", $parts["hour"], ":", $parts["minute"], ":", $parts["second"], PHP_EOL;

How It Works

  1. date_parse() reads the text and returns an array of its parts.
  2. The year, month, and day come from the date part of the text.
  3. The hour, minute, and second come from the time part.
  4. Each value is a plain integer, so the month 03 is shown as 3.

Example 2 — Missing Parts Are false

When the text has only a time, the date parts are not set.

PHP
<?php
$parts = date_parse("10:30");

echo "Year: ", var_export($parts["year"], true), PHP_EOL;
echo "Month: ", var_export($parts["month"], true), PHP_EOL;
echo "Hour: ", $parts["hour"], PHP_EOL;
echo "Minute: ", $parts["minute"], PHP_EOL;

How It Works

  1. The text 10:30 contains a time but no date.
  2. date_parse() does not guess today's date, so year and month are false.
  3. var_export() with true returns the value as text, so false can be printed.
  4. The hour and minute are found and returned as numbers.

Example 3 — Check a Date for Problems

Use the warning count and checkdate() to detect a date that does not exist.

PHP
<?php
$info = date_parse("2024-02-30");

echo "Errors: ", $info["error_count"], PHP_EOL;
echo "Warnings: ", $info["warning_count"], PHP_EOL;
echo reset($info["warnings"]), PHP_EOL;
echo checkdate($info["month"], $info["day"], $info["year"]) ? "Real date" : "Not a real date", PHP_EOL;

How It Works

  1. The text has a clear year, month, and day, so there are no errors.
  2. PHP adds a warning because February 30 does not exist.
  3. reset() returns the first warning message from the warnings array.
  4. checkdate() confirms that February 30, 2024 is not a real date.

Frequently Asked Questions

It reads a date and time string and returns an array with its year, month, day, hour, minute, and second, plus any warnings and errors. It does not create a date object.
date_create() builds a usable DateTime object, while date_parse() only shows how PHP understood the text. Use date_parse() when you want to inspect or validate input.
A value is false when the text does not contain that part. For example, parsing only a time leaves year, month, and day as false.
No. A date like 2024-02-30 only adds a warning. Combine it with checkdate() if you need to be sure the date really exists.
date_parse() works out the format on its own. date_parse_from_format() lets you give an exact format, such as d/m/Y, which is better for strict input.

Next: date_parse_from_format()

date_parse_from_format() reads a date string using a format you choose and returns its par.

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