PHP localtime() Function – Retrieve Local Time as an Array
The PHP localtime() function returns the local date and time as an array, optionally associative, mirroring the C library function, and accepts an optional Unix timestamp parameter, with examples showing both numeric and associative array outputs.
The localtime() function in PHP returns an array containing the local date and time, matching the structure of the C library's localtime call. If no arguments are provided, it uses the current time returned by time() .
Parameters
timestamp (optional): an integer Unix timestamp; defaults to the current time.
is_associative (optional): a boolean. When false or omitted, the function returns a numerically indexed array; when true , it returns an associative array with keys such as tm_sec , tm_min , etc.
Return value
By default, an array is returned. If the (undocumented) return_float option is set, a float may be returned instead.
Example usage
<?php
$localtime = localtime();
$localtime_assoc = localtime(time(), true);
print_r($localtime);
print_r($localtime_assoc);
?>The first print_r call outputs a numeric‑indexed array, e.g.:
Array(
[0] => 24
[1] => 3
[2] => 19
[3] => 3
[4] => 3
[5] => 105
[6] => 0
[7] => 92
)The second print_r call outputs an associative array with keys like tm_sec , tm_min , tm_hour , etc., for example:
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
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.