date_parse

Parses a date/time string and returns its components plus any parsing errors and warnings as an array.

Signature
date_parse(string $datetime): array
  • Added in PHP 5.2.0

Parameters

ParameterTypeDescription
$datetimestringA date/time string in any format accepted by DateTimeImmutable::__construct(), including relative expressions like "+3 days".

Return value

arrayAn associative array with year, month, day, hour, minute, second and fraction (each an int/float or false when absent), is_localtime and timezone details when a zone was given, an optional relative element, and the error_count, errors, warning_count and warnings elements.

Errors

Does not throw for unparsable input. Failures are reported in the errors and warnings elements of the returned array.

Changelog

  • PHP 7.2.0The zone element is now the UTC offset in seconds with the natural sign, instead of minutes with an inverted sign. An offset of +02:00 now reports 7200 where it used to report -120.

date_parse runs PHP's natural-language date parser and hands back everything it learned, including what it could not make sense of. Unlike strtotime, which collapses the whole answer into one timestamp or false, this returns every component separately and tells you which parts were absent.

When to reach for it

Reach for it when you need to validate a date string rather than use it. strtotime('2025-02-30') returns a perfectly usable timestamp for 2 March, giving no clue the input was wrong. date_parse on the same string returns a populated array with warning_count of 1 and the message "The parsed date was invalid". Checking both error_count and warning_count is the reliable way to reject bad input.

It is also useful for partial input. Given '14:30' the date fields come back as false rather than defaulting to today, so you can tell "the user gave a time only" from "the user gave a full datetime".

Reading the array

Missing components are false, not null and not 0, so use strict comparison when testing them. A relative expression such as '+3 days' populates a nested relative element instead of the absolute fields.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Validating a date string from a form or API and reporting exactly what was wrong
  • Detecting whether a user typed a date, a time, or both before deciding how to store it
  • Extracting the UTC offset a caller supplied without constructing a DateTime object

Frequently asked questions

What is the difference between date_parse and strtotime?

strtotime returns a single Unix timestamp or false, while date_parse returns every component separately along with the errors and warnings the parser produced. Use date_parse when you need to know why a string is bad.

How do I check if a date string is valid in PHP?

Call date_parse and require that both error_count and warning_count are 0. Checking error_count alone lets impossible dates such as 2025-02-30 pass.

Why are some elements of the date_parse array false?

Because that component was not present in the input string. date_parse does not fill in missing parts from the current date, which is exactly what makes it useful for validation.

More examples

A validator that catches what strtotime hides

strtotime returns a usable value for 2025-02-30, silently shifting it to 2 March, while date_parse flags it. Note that month 13 is a hard error and a relative phrase parses cleanly with no date components.

Example · php
<?php
function isRealDate(string $s): bool {
    $p = date_parse($s);
    return $p['error_count'] === 0 && $p['warning_count'] === 0;
}

foreach (['2025-02-28', '2025-02-30', '2025-13-01', 'next tuesday'] as $s) {
    printf("%-14s date_parse:%s  strtotime:%s\n", $s, isRealDate($s) ? 'ok  ' : 'bad ', strtotime($s) === false ? 'false' : 'value');
}

Telling partial input apart

Absent components come back as false rather than being filled in from today's date, so a strict !== false check distinguishes a date-only value from a time-only one.

Example · php
<?php
foreach (['2025-07-04', '14:30', '2025-07-04 14:30'] as $input) {
    $p = date_parse($input);
    $hasDate = $p['year'] !== false;
    $hasTime = $p['hour'] !== false;
    printf("%-18s date:%s time:%s\n", $input, $hasDate ? 'yes' : 'no ', $hasTime ? 'yes' : 'no');
}

Relative expressions land in their own element

No absolute fields are set because the string describes an offset, not a moment. The relative element holds the shifts that would be applied to a base date.

Example · php
<?php
$p = date_parse('+3 days 2 hours');

var_dump($p['year'], $p['hour']);
print_r($p['relative']);

Discussion

  • Be the first to comment on this lesson.