Home PHP Date/Time localtime() PHP localtime() Function 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.
$t = localtime(1700000000, true); Call What 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 localtime(?int $timestamp = null, bool $associative = false): array Parameters Parameter Description timestamp Optional. A Unix timestamp. If it is null or left out, the current time is used. associative Optional. 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 Value Description array The 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 1. Named Keys 2. Numbered Array and Count 3. Build a Normal Date String Example 1 — Named Keys Pass true as the second argument so you can read each part by name.
<?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 With true, the keys are tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday and tm_isdst. The time of day is easy: 22:13:20. tm_mon is 10 because November is the eleventh month and counting starts at 0. 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
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 localtime() always returns nine values. Index 4 is the month. It is still 10 for November.Index 5 is years since 1900, so adding 1900 gives 2023. Named keys are easier to read, so prefer the associative form. Frequently Asked Questions What does localtime() do in PHP? It returns the parts of a timestamp (seconds, minutes, hours, day, month, year and more) as an array, based on the default time zone.
Why is tm_year 123 for 2023? The value counts years since 1900, a rule inherited from the C library. Add 1900 to get the real year.
Why is tm_mon 10 for November? Months are numbered 0 to 11, so January is 0 and November is 10. Add 1 for the normal month number.
What is the difference between localtime() and getdate()? getdate() uses friendly values (month 1-12, full year, weekday names). localtime() uses the C-style offsets. For beginners, getdate() is easier.
How do I get named keys from localtime()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion