PHP localtime() Function

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

Definition

In PHP, localtime() returns the parts of a date and time as an array, using the names from the C 'tm' structure.

It works like getdate(), but the keys and values follow the C library. For example, the month starts at 0 and the year counts from 1900. Pass true as the second argument to get named keys.

Syntax

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

PHP
$t = localtime(1700000000, true);
CallWhat it does
localtime(1700000000, true)Returns an array with keys like tm_hour, tm_mon and tm_year.
localtime(1700000000)Returns a plain numbered array with the same nine values.
localtime(1700000000, true)['tm_year']Returns 123, which is 2023 minus 1900.

The values are zero-based in surprising ways: tm_mon is 0-11 (0 is January), tm_year is the number of years since 1900, tm_wday is 0-6 (0 is Sunday) and tm_yday starts at 0. Add 1 to the month and 1900 to the year to get everyday numbers.

Library declarations
PHP
localtime(?int $timestamp = null, bool $associative = false): array

Parameters

ParameterDescription
timestampOptional. A Unix timestamp. If it is null or left out, the current time is used.
associativeOptional. If true, the array has named keys (tm_sec, tm_min, ...). If false (the default), it is a numbered array starting at 0.

Return Value

Return ValueDescription
arrayThe nine values: seconds, minutes, hours, day of month, month (0-11), years since 1900, weekday (0-6), day of year (0-365) and daylight saving flag.

PHP Examples

Example 1 — Named Keys

Pass true as the second argument so you can read each part by name.

PHP
<?php
date_default_timezone_set('UTC');

$t = localtime(1700000000, true);

echo $t['tm_hour'], ':', $t['tm_min'], ':', $t['tm_sec'], PHP_EOL;
echo $t['tm_mday'], PHP_EOL;
echo $t['tm_mon'], PHP_EOL;
echo $t['tm_year'], PHP_EOL;

How It Works

  1. With true, the keys are tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday and tm_isdst.
  2. The time of day is easy: 22:13:20.
  3. tm_mon is 10 because November is the eleventh month and counting starts at 0.
  4. tm_year is 123 because 2023 - 1900 = 123.

Example 2 — Numbered Array and Count

Without the second argument, you get a numbered array. The month sits at index 4 and the year at index 5.

PHP
<?php
date_default_timezone_set('UTC');

$t = localtime(1700000000);

echo count($t), PHP_EOL;
echo $t[4], PHP_EOL;
echo $t[5] + 1900, PHP_EOL;

How It Works

  1. localtime() always returns nine values.
  2. Index 4 is the month. It is still 10 for November.
  3. Index 5 is years since 1900, so adding 1900 gives 2023.
  4. Named keys are easier to read, so prefer the associative form.

Example 3 — Build a Normal Date String

Fix the zero-based values and print a standard date.

PHP
<?php
date_default_timezone_set('UTC');

$t = localtime(1700000000, true);

echo sprintf('%04d-%02d-%02d', $t['tm_year'] + 1900, $t['tm_mon'] + 1, $t['tm_mday']), PHP_EOL;

How It Works

  1. Add 1900 to tm_year to get the real year.
  2. Add 1 to tm_mon to get the real month number.
  3. tm_mday is already correct because the day starts at 1.
  4. sprintf() pads the month and day with leading zeros.

Frequently Asked Questions

It returns the parts of a timestamp (seconds, minutes, hours, day, month, year and more) as an array, based on the default time zone.
The value counts years since 1900, a rule inherited from the C library. Add 1900 to get the real year.
Months are numbered 0 to 11, so January is 0 and November is 10. Add 1 for the normal month number.
getdate() uses friendly values (month 1-12, full year, weekday names). localtime() uses the C-style offsets. For beginners, getdate() is easier.
Pass true as the second argument, for example localtime($ts, true). Otherwise you get a numbered array.

Next: microtime()

microtime() returns the current Unix time with microsecond precision, as a string or a flo.

microtime() 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