localtime()

localtime() — Get the local time.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

The localtime() function returns an array identical to that of the structure returned by the C function call.

Signature

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

Parameters

ParameterTypeWhat it takes
$timestamp?intThe optional timestamp parameter is an int Unix timestamp that defaults to the current local time if timestamp is omitted or null. In other words, it defaults to the value of time().
$associativeboolDetermines whether the function should return a regular, numerically indexed array, or an associative one.

Return value

If associative is set to false or not supplied then the array is returned as a regular, numerically indexed array. If associative is set to true then localtime() returns an associative array containing the elements of the structure returned by the C function call to localtime. The keys of the associative array are as follows: "tm_sec" - seconds, 0 to 59 "tm_min" - minutes, 0 to 59 "tm_hour" - hours, 0 to 23 "tm_mday" - day of the month, 1 to 31 "tm_mon" - month of the year, 0 (Jan) to 11 (Dec) "tm_year" - years since 1900 "tm_wday" - day of the week, 0 (Sun) to 6 (Sat) "tm_yday" - day of the year, 0 to 365 "tm_isdst" - is daylight savings time in effect? Positive if yes, 0 if not, negative ...

Example

A runnable snippet:

$localtime = localtime(); $localtime_assoc = localtime(time(), true); print_r($localtime); print_r($localtime_assoc);

Output:

Array ( [0] => 24 [1] => 3 [2] => 19 [3] => 3 [4] => 3 [5] => 105 [6] => 0 [7] => 92 [8] => 1 ) Array ( [tm_sec] => 24 [tm_min] => 3 [tm_hour] => 19 [tm_mday] => 3 [tm_mon] => 3 [tm_year] => 105 [tm_wday] => 0 [tm_yday] => 92 [tm_isdst] => 1 )

Changelog

VersionChange
8.0.0timestamp is nullable now.

See also

getdate()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page localtime() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.