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] => 8
    [1] => 26
    [2] => 15
    [3] => 15
    [4] => 0
    [5] => 125
    [6] => 3
    [7] => 14
    [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] => 8
    [tm_min] => 26
    [tm_hour] => 15
    [tm_mday] => 15
    [tm_mon] => 0
    [tm_year] => 125
    [tm_wday] => 3
    [tm_yday] => 14
    [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] => 8
    [tm_min] => 56
    [tm_hour] => 20
    [tm_mday] => 15
    [tm_mon] => 0
    [tm_year] => 125
    [tm_wday] => 3
    [tm_yday] => 14
    [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: 1, Day: 15
Hour : 20, Minutes: 56, Sec: 8

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: 1, Day: 15
    Hour : 10, Minutes: 26, Sec: 8

    Year: 2025, Month: 1, Day: 15
    Hour : 20, Minutes: 56, Sec: 8

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


    Subscribe

    * indicates required
    Subscribe to plus2net

      plus2net.com











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