localtime() returns numeric date and time parts for a Unix timestamp. Pass true to receive an associative array with named keys.
date_default_timezone_set('Asia/Kolkata');
$parts = localtime(time(), true);
echo $parts['tm_hour'] . ':' . $parts['tm_min'];This current example appears first; the detailed examples below are retained and modernized for comparison and additional use cases.
localtime();
Example code
print_r(localtime());
The output of above code is here
Array
(
[0] => 56
[1] => 49
[2] => 2
[3] => 30
[4] => 8
[5] => 126
[6] => 3
[7] => 272
[8] => 0
)
We can get better output by using is_associative to true. We will get a set of keys and associated data. Here is an example.
print_r(localtime(time(),true));
The output is here. Array
(
[tm_sec] => 56
[tm_min] => 49
[tm_hour] => 2
[tm_mday] => 30
[tm_mon] => 8
[tm_year] => 126
[tm_wday] => 3
[tm_yday] => 272
[tm_isdst] => 0
)
We can change the time zone set and check the return like this.
date_default_timezone_set('Asia/Kolkata');
print_r(localtime(time(),true));
The ouput is hereArray
(
[tm_sec] => 56
[tm_min] => 19
[tm_hour] => 8
[tm_mday] => 30
[tm_mon] => 8
[tm_year] => 126
[tm_wday] => 3
[tm_yday] => 272
[tm_isdst] => 0
)
Refresh this page and see how the data is changing$time = localtime(time(), true);
echo "Year: " . ($time['tm_year'] + 1900) . ", Month: " . ($time['tm_mon'] + 1) . ", Day: " . $time['tm_mday'];
echo "<br>";
echo "Hour : " . ($time['tm_hour'] ) . ", Minutes: " . ($time['tm_min']) . ", Sec: " . $time['tm_sec'];
output
Year: 2026, Month: 9, Day: 30
Hour : 8, Minutes: 19, Sec: 56
$timestamp = mktime(14, 30, 0, 7, 4, 2025);
$time = localtime($timestamp, true);
print_r($time);
Explanation:
date_default_timezone_set('America/New_York');
$time = localtime(time(), true);
echo 'Year: ' . ($time['tm_year'] + 1900) . ', Month: ' . ($time['tm_mon'] + 1) . ', Day: ' . $time['tm_mday'];
echo '<br>';
echo 'Hour : ' . ($time['tm_hour'] ) . ', Minutes: ' . ($time['tm_min']) . ', Sec: ' . $time['tm_sec'];
date_default_timezone_set('Asia/Kolkata');
$time = localtime(time(), true);
echo '<br><br>';
echo 'Year: ' . ($time['tm_year'] + 1900) . ', Month: ' . ($time['tm_mon'] + 1) . ', Day: ' . $time['tm_mday'];
echo '<br>';
echo 'Hour : ' . ($time['tm_hour'] ) . ', Minutes: ' . ($time['tm_min']) . ', Sec: ' . $time['tm_sec'];
Output is here
Year: 2026, Month: 9, Day: 29
Hour : 22, Minutes: 49, Sec: 56
Year: 2026, Month: 9, Day: 30
Hour : 8, Minutes: 19, Sec: 56
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.