localtime() : returns an array with details about the current local time

We can get local time with date, month year etc. ( array output ) by using localtime() function in PHP.
Here is the syntax.
localtime();
Example code
print_r(localtime());
The output of above code is here
Array
(
    [0] => 49
    [1] => 42
    [2] => 3
    [3] => 14
    [4] => 9
    [5] => 125
    [6] => 2
    [7] => 286
    [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] => 49
    [tm_min] => 42
    [tm_hour] => 3
    [tm_mday] => 14
    [tm_mon] => 9
    [tm_year] => 125
    [tm_wday] => 2
    [tm_yday] => 286
    [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 here
Array
(
    [tm_sec] => 49
    [tm_min] => 12
    [tm_hour] => 9
    [tm_mday] => 14
    [tm_mon] => 9
    [tm_year] => 125
    [tm_wday] => 2
    [tm_yday] => 286
    [tm_isdst] => 0
)
Refresh this page and see how the data is changing

Example: Formatting Local Time

$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: 2025, Month: 10, Day: 14
Hour : 9, Minutes: 12, Sec: 49

Example: Getting Time for a Specific Timestamp

$timestamp = mktime(14, 30, 0, 7, 4, 2025);
$time = localtime($timestamp, true);
print_r($time);
Explanation:
  • Formatting Local Time: Shows how to retrieve and format the current local time.
  • Handling Timestamps: Demonstrates how to get local time information from a specific timestamp.

  • We can set different time zones by using date_default_timezone_set() and check the local time.
    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: 2025, Month: 10, Day: 13
    Hour : 23, Minutes: 42, Sec: 49

    Year: 2025, Month: 10, Day: 14
    Hour : 9, Minutes: 12, Sec: 49

    PHP Date Functions GMT/UTC date/time Date formats for Date()
    Subscribe to our YouTube Channel here



    plus2net.com











    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
    ©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer