Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Date and Time

PHP date_parse() and getdate() functions

Reference for PHP date_parse(), date_parse_from_format() and getdate(): turn date text or a timestamp into an array of parts and detect parse errors.

date_parse() reads a date string and returns an array of its parts: year, month, day, hour, minute, second, fraction, plus lists of warnings and errors. Unlike strtotime() it does not give you a timestamp; it tells you what the parser understood, and leaves fields it did not find as false. date_parse_from_format() does the same for a known format.

getdate() goes the other way round. It takes a Unix timestamp and returns an array with the broken-down parts in the default time zone, including the weekday and month names.

PHP
$p = date_parse('2024-03-09 14:05:07.5');
echo $p['year'], '-', $p['month'], '-', $p['day']; // 2024-3-9

Try it

PHP date_parse() on good and bad input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP getdate() and date_parse_from_format()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
date_parse(string $datetime): array
date_parse_from_format(string $format, string $datetime): array
getdate(?int $timestamp = null): array
Name Description
$datetime The date text to parse, in any format strtotime() understands.
$format A format string using the same letters as DateTime::createFromFormat().
$timestamp Unix timestamp. When null (or omitted) the current time is used.

Return value:

  • date_parse() and date_parse_from_format() return an array with year, month, day, hour, minute, second, fraction (each an int/float or false if absent), warning_count, warnings, error_count, errors, is_localtime, and, when relative text or a zone was found, relative and zone_type/zone keys.
  • getdate() returns an array with seconds, minutes, hours, mday, wday (0 is Sunday), mon, year, yday, weekday, month and the key 0 holding the timestamp.

Errors: none of these throw for bad input. Read error_count and errors instead. A date that is well formed but impossible (such as February 30) is a warning, not an error.

Usage notes

  • Absent fields are false, not 0. date_parse('2024-03-09')['hour'] is false. Test with === false, not with !.
  • Check error_count before trusting the data. The arrays always come back, even for nonsense.
  • Warnings flag impossible dates. 2024-02-30 parses to day 30 with a warning ("The parsed date was invalid"); to validate a date, use checkdate() or DateTime::getLastErrors().
  • Relative text is not resolved. next monday stays in the relative sub-array; only strtotime() or DateTime resolve it against "now".
  • getdate() uses the default time zone (date_default_timezone_set()). The names are English.
  • Prefer objects in new code. DateTimeImmutable::createFromFormat() plus getLastErrors() replaces date_parse_from_format() when you also need a usable date; date_parse* shines when you only need to inspect or validate parts.
  • localtime() is a similar older function with numeric keys; most code can use getdate() or DateTime::format().