PHP localtime() Function

What is PHP localtime() Function?

If you want to know time detail of the current local time (which is determined by the server’s default timezone settings), use PHP localtime() function. you can also know the time detail of a specific timestamp.

If you want to know time detail of the followings, use PHP localtime() function-

  • current local time of your server’s default timezone.
  • current local time of a specific timezone.
  • current local time of a timestamp.

The function returns the following time detail of a current local time-

Numeric keyAssociative KeyDescriptionValue
0tm_secSeconds0 to 59
1tm_minMinutes0 to 59
2tm_hourHours0 to 23
3tm_mdayday of the month1 to 31
4tm_monmonth of the year0 (Jan) to 11 (Dec)
5tm_yearYearssince 1900
6tm_wdayday of the week0 (Sun) to 6 (Sat)
7tm_ydayday of the year0 to 365
8tm_isdstis daylight savings time in effect?1 (yes), 0 (no), -1 (unknown)

Note: When the function returns an indexed array, the function has its key like the 1st column “Numeric key”. Similarly, when the function returns an associative array, the function has its key like the 2nd column “Associative key”.

Syntax:

localtime(timestamp, is_associative)

Parameters:

The Function has 2 parameters which are both optional –

timestamp (Optional): it specifies an Unix timestamp. If omitted, the function calculates it from the local server’s default timezone (Check example 1).

is_associative (Optional): it specifies a Boolean which indicates whether you want to time detail in an indexed array or in an associative array.

  • TRUE – the function returns time detail in an associative array (Check example 1).
  • FALSE – the function returns time detail in an indexed array.

Return Values:

The function returns an array that contains time detail.

Examples:

Example 1:

<pre>
<?php
echo "The local time is: <br />";
print_r(localtime());

echo "The local time is: <br />";
print_r(localtime(time(), TRUE));
?>
</pre>

Output:

The local time is: 
Array
(
[0] => 47
[1] => 46
[2] => 16
[3] => 11
[4] => 0
[5] => 126
[6] => 0
[7] => 10
[8] => 0
)
The local time is:
Array
(
[tm_sec] => 47
[tm_min] => 46
[tm_hour] => 16
[tm_mday] => 11
[tm_mon] => 0
[tm_year] => 126
[tm_wday] => 0
[tm_yday] => 10
[tm_isdst] => 0
)

Example 2:

<pre>
<?php
// Setting a timezone to see its current local time-
date_default_timezone_set("Europe/London");
$associative_time = localtime(time(), true);
echo "The current local time detail of London is: <br />";
print_r($associative_time);
?>
</pre>

Output:

The current local time detail of London is: 
Array
(
[tm_sec] => 15
[tm_min] => 49
[tm_hour] => 15
[tm_mday] => 11
[tm_mon] => 0
[tm_year] => 126
[tm_wday] => 0
[tm_yday] => 10
[tm_isdst] => 0
)

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP localtime() Function

The gettimeofday() function may be the most commonly used built-in PHP date and time function. Use this function to know detail of your current local time.

Reference:

https://www.php.net/manual/en/function.localtime.php