PHP getdate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, getdate() turns a Unix timestamp into an array that holds the date and time parts, such as the year, month, day and hour.

Instead of formatting a string, you get a ready-made array. You can read $info['year'], $info['mon'] or $info['weekday'] and use each piece on its own. If you skip the timestamp, PHP uses the current time.

Syntax

Call getdate() with the values you need. Start with this line.

PHP
$info = getdate(1700000000);
CallWhat it does
getdate(1700000000)['weekday']Returns "Tuesday" when the default time zone is UTC.
getdate(0)['year']Returns 1970, the year of the Unix epoch.
getdate()Returns the parts of the current date and time.

getdate() uses the default time zone set by date_default_timezone_set(). The same timestamp can give a different day or hour in another time zone. The array also has the numeric timestamp at index 0.

Library declarations
PHP
getdate(?int $timestamp = null): array

Parameters

ParameterDescription
timestampOptional. A Unix timestamp (seconds since 1 January 1970 UTC). If it is null or left out, the current time is used.

Return Value

Return ValueDescription
arrayAn associative array with the keys seconds, minutes, hours, mday, wday, mon, year, yday, weekday, month and 0 (the timestamp itself).

PHP Examples

Example 1 — Read the Parts of a Date

Pass a fixed timestamp to getdate() and print the pieces you need.

PHP
<?php
date_default_timezone_set('UTC');

$d = getdate(1700000000);

echo $d['weekday'], ', ', $d['month'], ' ', $d['mday'], ', ', $d['year'], PHP_EOL;
echo $d['hours'], ':', $d['minutes'], ':', $d['seconds'], PHP_EOL;

How It Works

  1. date_default_timezone_set('UTC') makes the result the same on every server.
  2. getdate(1700000000) returns an array describing 14 November 2023, 22:13:20.
  3. weekday and month are full English names, while mday and year are numbers.
  4. hours, minutes and seconds hold the time of day.

Example 2 — All Keys and the Timestamp at Index 0

See which keys the array contains. The original timestamp is stored under the numeric key 0.

PHP
<?php
date_default_timezone_set('UTC');

$d = getdate(1700000000);

echo implode(', ', array_keys($d)), PHP_EOL;
echo $d[0], PHP_EOL;

How It Works

  1. array_keys() lists every key in the order PHP creates them.
  2. implode() joins the keys into one readable line.
  3. The last key is the number 0, not a word.
  4. $d[0] gives back the same timestamp you passed in.

Example 3 — Day of Year and Weekday Check

Use yday and wday to build simple messages about a date.

PHP
<?php
date_default_timezone_set('UTC');

$d = getdate(1700000000);

echo 'Day ', $d['yday'] + 1, ' of the year', PHP_EOL;
echo in_array($d['wday'], [0, 6]) ? 'It is a weekend' : 'It is a weekday', PHP_EOL;

How It Works

  1. yday is 317 for this date because it starts counting at 0.
  2. Adding 1 gives the more natural day number, 318.
  3. wday is 2 (Tuesday). Sunday is 0 and Saturday is 6.
  4. in_array() checks whether wday is a weekend value.

Frequently Asked Questions

It returns an associative array with the parts of a date and time: seconds, minutes, hours, mday, wday, mon, year, yday, weekday, month, and the timestamp at index 0.
date() returns one formatted string, such as '2023-11-14'. getdate() returns an array so you can use each part separately without parsing a string.
Yes. It uses the default time zone from date_default_timezone_set() or php.ini. The same timestamp can show a different day or hour in another zone.
yday counts from 0, so 1 January is 0 and 31 December is 364 (or 365 in a leap year). Add 1 if you need the normal day-of-year number.
PHP uses the current time. This is the same as calling getdate(time()).

Next: gettimeofday()

gettimeofday() returns the current time with microsecond precision, either as an array or .

gettimeofday() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful