W3docs

localtime()

Sure thing! localtime() is a built-in PHP function that converts a Unix timestamp into an array of local time values. This array contains information such as

Warning: localtime() was deprecated in PHP 7.0 and removed in PHP 8.0. It is no longer available in modern PHP. For new code, use the DateTime class, or date() together with date_default_timezone_set(). The examples below are kept for readers maintaining legacy PHP 5.x/7.x code.

localtime() was a built-in PHP function that broke a Unix timestamp down into the individual components of the local date and time — second, minute, hour, day, month, year, and so on. It mirrored the C library's localtime() call and returned a 9-element array rather than a formatted string, which made it useful when you needed to read or compute individual fields (for example, "is it currently a weekend?") rather than print a date.

Because it returned the local time, the result depended on the server's configured timezone. To make output predictable across machines, set the timezone explicitly with date_default_timezone_set() before calling it.

Syntax

<?php

localtime(int $timestamp = time(), bool $associative = false): array

The timestamp parameter is the Unix timestamp you want to convert (it defaults to the current time via time()). associative is an optional Boolean that determines whether the returned array is keyed by name or by position.

Indexed (default) array

If associative is false or omitted, the array is indexed numerically and contains these values, in order:

  • 0: Seconds (0-59).
  • 1: Minutes (0-59).
  • 2: Hours (0-23).
  • 3: Day of the month (1-31).
  • 4: Month (0-11, where 0 is January).
  • 5: Years since 1900.
  • 6: Day of the week (0-6, where 0 is Sunday).
  • 7: Day of the year (0-365).
  • 8: Whether Daylight Saving Time is in effect (1 if yes, 0 if no, -1 if unknown).

Gotcha: Two of these fields are easy to misread. The month index 4 is zero-based (0 = January, 11 = December), so add 1 for a human-readable month. The year (index 5) counts years since 1900, so add 1900 to get the calendar year.

Associative array

If associative is set to true, the array keys are named after the C struct tm fields instead of integers, which makes the result self-documenting:

array(
  'tm_sec'   => ...,  // seconds (0-59)
  'tm_min'   => ...,  // minutes (0-59)
  'tm_hour'  => ...,  // hours (0-23)
  'tm_mday'  => ...,  // day of the month (1-31)
  'tm_mon'   => ...,  // month (0-11)
  'tm_year'  => ...,  // years since 1900
  'tm_wday'  => ...,  // day of the week (0-6, Sunday = 0)
  'tm_yday'  => ...,  // day of the year (0-365)
  'tm_isdst' => ...,  // daylight saving flag (1, 0, or -1)
)

Examples

<?php

// Get the current local time
$now = time();
$localtime = localtime($now);
print_r($localtime);

// Get the local time for a specific Unix timestamp
$timestamp = 1646563200; // March 5, 2022, 12:00:00 AM UTC
$localtime = localtime($timestamp);
print_r($localtime);

// Get the local time as an associative array
$now = time();
$localtime = localtime($now, true);
echo "The current year is " . ($localtime['tm_year'] + 1900);

These examples show how you can use localtime() to convert Unix timestamps into local time values and read individual fields as needed.

The modern replacement

Since localtime() is gone in PHP 8, the recommended way to break a timestamp into its parts is getdate() (which already returns a friendly associative array with mon, year, wday, etc.) or the object-oriented DateTime:

<?php

date_default_timezone_set('UTC');

$parts = getdate(1646438400); // 2022-03-05 00:00:00 UTC
echo $parts['mday'] . '-' . $parts['mon'] . '-' . $parts['year']; // 5-3-2022

$dt = new DateTime('@1646438400');
echo $dt->format('Y-m-d H:i:s'); // 2022-03-05 00:00:00

Unlike localtime(), getdate() gives you year and mon directly (no +1900 or +1 arithmetic), and DateTime lets you format or do date math without juggling array indexes.

See also

  • getdate() — the supported replacement that returns a labelled date array.
  • mktime() — build a timestamp from individual local-time components (the inverse operation).
  • strtotime() — parse a human-readable date string into a timestamp.
  • PHP Date and Time — overview of PHP's date/time tools.

Practice

Practice
What is the function of localtime() in PHP?
What is the function of localtime() in PHP?
Was this page helpful?