getdate()
Learn the PHP getdate() function: syntax, parameters, the associative array it returns, and runnable examples for working with dates and timestamps.
Introduction
The PHP getdate() function breaks a date down into its individual parts — year, month, day, hour, weekday name, and more — and returns them as an associative array. By default it describes the current local time, but you can pass any Unix timestamp to describe a different moment instead.
A Unix timestamp is an integer counting the number of seconds since January 1, 1970, 00:00:00 UTC (the "Unix epoch"). Functions like time(), mktime(), and strtotime() all produce such timestamps, and getdate() turns one into a human-readable breakdown.
This page covers the syntax, every key in the returned array, runnable examples, and how getdate() compares to the modern DateTime class.
Syntax and Parameters
The syntax for getdate() is as follows:
getdate(int $timestamp = time()): arrayThe single, optional parameter $timestamp is the Unix timestamp to describe. If you omit it, getdate() defaults to time() — the current moment. The result reflects the configured timezone (see date_default_timezone_set(), or the date.timezone setting in php.ini), so set your timezone if you need consistent results across servers.
Return Value
getdate() returns an associative array describing the given timestamp. Here is a breakdown of every key:
| Key | Value |
|---|---|
| "seconds" | Seconds (0-59) |
| "minutes" | Minutes (0-59) |
| "hours" | Hours (0-23) |
| "mday" | Day of the Month (1-31) |
| "wday" | Day of the Week (0-6, 0=Sunday) |
| "mon" | Month (1-12) |
| "year" | Year (e.g., 2023) |
| "yday" | Day of the Year (0-365) |
| "weekday" | Full weekday name (e.g., "Monday") |
| "month" | Full month name (e.g., "January") |
| "0" | Unix timestamp (seconds since Jan 1 1970) |
| "zone" | Timezone offset in seconds from GMT |
Note: the "0" key (the raw timestamp) is an integer; the rest are integers except "weekday" and "month", which are strings. The "is_dst" key was deprecated in PHP 7.0 and removed in PHP 8.0.
Usage and Examples
Formatting the current date
Call getdate(), store the result, and read whichever keys you need:
This outputs something like: Today is Thursday, March 3, 2023.
Describing a specific timestamp
Pass a Unix timestamp to describe a moment other than now. Here we use a fixed timestamp so the output is reproducible:
<?php
// 2021-12-25 00:00:00 UTC
$date = getdate(1640390400);
echo $date['weekday'] . ", " . $date['mday'] . " " . $date['month'] . " " . $date['year'] . PHP_EOL;
echo "Day " . $date['yday'] . " of the year, hour " . $date['hours'] . PHP_EOL;Assuming a UTC timezone, this prints:
Saturday, 25 December 2021
Day 358 of the year, hour 0Building a timestamp first, then breaking it down
getdate() pairs naturally with mktime(), which builds a timestamp from individual parts:
<?php
// mktime(hour, minute, second, month, day, year)
$timestamp = mktime(9, 30, 0, 7, 4, 2024);
$parts = getdate($timestamp);
echo "{$parts['weekday']} at {$parts['hours']}:{$parts['minutes']}";This prints Thursday at 9:30.
getdate() vs. the DateTime class
getdate() is convenient for quick, one-off reads, but for anything involving arithmetic, comparison, or timezones the object-oriented DateTime class is the modern choice:
DateTimehandles date math (->add(),->sub(),->diff()) safely across month and year boundaries.- It carries an explicit timezone, avoiding surprises from the global default.
- It formats output with
date-formatinstead of manual string concatenation.
Reach for getdate() when you just need a few fields from a timestamp; reach for DateTime when you need to manipulate or compare dates.
Conclusion
getdate() turns a Unix timestamp into a labeled associative array, making it a quick way to pull out the year, month, weekday, or any other component of a date. For display and simple lookups it is ideal; for date arithmetic and timezone-aware logic, prefer the DateTime class.