getdate
Returns an array of date and time components for a Unix timestamp in the default timezone.
getdate(?int $timestamp = null): array- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$timestampoptional | ?int= null | A Unix timestamp in seconds. When omitted or null the current time is used. |
Return value
arrayAn associative array with the keys seconds, minutes, hours, mday, wday, mon, year, yday, weekday and month, plus key 0 holding the timestamp itself.
Errors
Does not throw.
Changelog
- PHP 8.0The timestamp parameter became nullable. Passing null now means the current time; in PHP 7 an explicit null was cast to 0.
getdate breaks a timestamp into every part at once and returns them as a named array, interpreted in the script's default timezone. One call gives you the year, the month as both a number and a name, the day of month, the day of week as both a number and a name, and the day of year.
When to reach for it
It earns its place when a single moment needs several of its parts. Building a calendar grid, for example, needs the weekday index of the first of the month and the number of days in it; pulling those with one getdate call is clearer than four separate date calls that each re-parse the timestamp.
For a single component, date('Y') is shorter and more direct. For anything involving arithmetic or another timezone, DateTimeImmutable is the better tool.
Things that trip people up
yday is zero-based: 1 January is day 0, not day 1. wday is also zero-based and Sunday-first, which does not match the ISO convention where Monday is 1. Element 0 is the timestamp, so a naive foreach over the array will visit it alongside the named keys.
Example
When to use it
- Working out which weekday column the first of the month falls in when rendering a calendar
- Grouping records by year and month without calling date twice per row
- Printing a readable timestamp breakdown in a debug dump
Frequently asked questions
What is the difference between getdate and date?
getdate returns all the components at once in an array, while date returns one formatted string you control with a format. Use getdate when you need several parts of the same moment.
Why is getdate's yday off by one?
It is not off by one — yday is deliberately zero-based, so 1 January is 0 and 31 December is 364 or 365. Add 1 if you want a human-readable day-of-year number.
Does getdate use UTC?
No, it uses the script's default timezone. Call date_default_timezone_set first, or use gmdate if you specifically want UTC components.
More examples
Laying out the first week of a month
wday gives the Sunday-based column index of the 1st, which is exactly the number of empty cells a calendar grid needs before it. Element 0 is reused as the timestamp for the date('t') lookup.
<?php
date_default_timezone_set('UTC');
$first = getdate(mktime(0, 0, 0, 7, 1, 2025));
$daysInMonth = (int) date('t', $first[0]);
echo "{$first['month']} {$first['year']} starts on {$first['weekday']}\n";
echo 'leading blank cells: ', $first['wday'], "\n";
echo 'days in month: ', $daysInMonth, "\n";
echo 'grid rows needed: ', (int) ceil(($first['wday'] + $daysInMonth) / 7), "\n";getdate versus the equivalent date calls
wday matches date('w') and yday matches date('z'), both zero-based. date('N') is the ISO alternative where Monday is 1 and Sunday is 7.
<?php
date_default_timezone_set('UTC');
$ts = 1751630400;
$g = getdate($ts);
printf("getdate wday=%d yday=%d\n", $g['wday'], $g['yday']);
printf("date w=%s z=%s N=%s\n", date('w', $ts), date('z', $ts), date('N', $ts));
printf("month mon=%d %s / date n=%s M=%s\n", $g['mon'], $g['month'], date('n', $ts), date('M', $ts));The timezone decides the answer
The same instant is Saturday the 5th in UTC and still Friday the 4th in Los Angeles. getdate reads the default timezone, so changing it changes every element of the array.
<?php
$ts = 1751673600; // 2025-07-05 00:00:00 UTC
date_default_timezone_set('UTC');
$utc = getdate($ts);
date_default_timezone_set('America/Los_Angeles');
$la = getdate($ts);
echo "UTC: {$utc['weekday']} {$utc['mday']} {$utc['month']}\n";
echo "LA: {$la['weekday']} {$la['mday']} {$la['month']}\n";
Discussion