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.
$p = date_parse('2024-03-09 14:05:07.5'); echo $p['year'], '-', $p['month'], '-', $p['day']; // 2024-3-9
Try it
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.
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
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()anddate_parse_from_format()return an array withyear,month,day,hour,minute,second,fraction(each an int/float orfalseif absent),warning_count,warnings,error_count,errors,is_localtime, and, when relative text or a zone was found,relativeandzone_type/zonekeys.getdate()returns an array withseconds,minutes,hours,mday,wday(0 is Sunday),mon,year,yday,weekday,monthand the key0holding 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']isfalse. Test with=== false, not with!. - Check
error_countbefore trusting the data. The arrays always come back, even for nonsense. - Warnings flag impossible dates.
2024-02-30parses to day 30 with a warning ("The parsed date was invalid"); to validate a date, usecheckdate()orDateTime::getLastErrors(). - Relative text is not resolved.
next mondaystays in therelativesub-array; onlystrtotime()orDateTimeresolve it against "now". getdate()uses the default time zone (date_default_timezone_set()). The names are English.- Prefer objects in new code.
DateTimeImmutable::createFromFormat()plusgetLastErrors()replacesdate_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 usegetdate()orDateTime::format().
Related
strtotime(),checkdate(),mktime(),date(), Date format characters- Lessons: Date and time