localtime

Breaks a Unix timestamp into its local date and time components as an array.

Signature
localtime(?int $timestamp = null, bool $associative = false): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$timestampoptional?int= nullThe Unix timestamp to decompose. Defaults to the value of time() when omitted or null.
$associativeoptionalbool= falseWhen true the result is keyed with tm_* names; when false it is a numerically indexed array in struct tm order.

Return value

arrayAn array of the date/time components in local time: numerically indexed by default, or keyed with tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday and tm_isdst when associative is true.

Errors

Emits E_WARNING if the default time zone is not valid.

Changelog

  • PHP 8.0.0timestamp is nullable now.

localtime() is a thin wrapper over the C library's localtime() call, and the array it returns mirrors the C struct tm exactly — including the two conventions that surprise PHP developers.

When to reach for it

Reach for it when you are porting C or Perl code that already speaks struct tm, or when you want the day-of-year and daylight-saving flag together with the rest of the components in one call. For general PHP work, getdate() returns the same information with friendlier keys (mon is 1-12, year is the real year), and DateTimeImmutable is better still when time zones are involved.

What trips people up

Two fields do not mean what their names suggest. tm_mon counts from 0 for January, so you must add 1. tm_year is years since 1900, so 2025 comes back as 125 and you must add 1900. Reassembling a date without those adjustments produces a January date in the year 125.

Called with no arguments it returns the numerically indexed form for the current time, which is almost never what you want to read in code — pass true as the second argument so the fields are self-describing. The conversion uses the default time zone set by date_default_timezone_set(), not the system clock's zone.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Port C or legacy Perl date handling that already expects struct tm fields
  • Read the day-of-year and daylight-saving flag for a timestamp in one call
  • Split a stored timestamp into hour and minute buckets for a scheduling report

Frequently asked questions

What is the difference between localtime and getdate?

getdate returns human-friendly values (mon is 1-12, year is the full year, plus month and weekday names), while localtime mirrors the C struct tm with a zero-based tm_mon and tm_year counted from 1900. Prefer getdate in new PHP code.

Why does localtime return 125 for the year?

tm_year is defined as years since 1900, so 2025 is 125. Add 1900 to get the calendar year.

Which time zone does localtime use?

The script's default time zone, set with date_default_timezone_set or the date.timezone INI setting, not the operating system's zone.

More examples

The numerically indexed form

Without the second argument you get positions, not names: index 0 is seconds and index 2 is the hour. The output shows [4] => 11 for December and [5] => 125 for 2025, the two offset fields.

Example · php
<?php
date_default_timezone_set('UTC');
$ts = mktime(23, 59, 58, 12, 31, 2025);

$t = localtime($ts); // associative defaults to false
print_r($t);

// order is sec, min, hour, mday, mon, year, wday, yday, isdst
echo "seconds: {$t[0]}, hour: {$t[2]}\n";

Comparing localtime with getdate

The same instant prints as 1/124 through localtime and 2/2024 through getdate. getdate also supplies month and weekday names, which localtime never does.

Example · php
<?php
date_default_timezone_set('UTC');
$ts = mktime(12, 0, 0, 2, 29, 2024);

$lt = localtime($ts, true);
$gd = getdate($ts);

echo 'localtime month/year: ', $lt['tm_mon'], '/', $lt['tm_year'], "\n";
echo 'getdate month/year:   ', $gd['mon'], '/', $gd['year'], "\n";
echo 'getdate weekday name: ', $gd['weekday'], "\n";

Discussion

  • Be the first to comment on this lesson.