PHP Function Reference

PHP localtime() Function



The PHP localtime() function returns the local time. The array by this function is identical to that of the structure returned by the C function call.

Syntax

localtime(timestamp, associative)

Parameters

timestamp Optional. Specify a Unix timestamp representing the date. If it is omitted or null, it defaults to the current local time.
associative Optional. If set to true the function returns an associative array otherwise returns a numerically indexed array. Default is False.

Return Value

If associative is set to true the function returns an associative array otherwise returns a numerically indexed array. The keys of the associative array are as follows:

  • [tm_sec] - seconds, 0 to 59
  • [tm_min] - minutes, 0 to 59
  • [tm_hour] - hours, 0 to 23
  • [tm_mday] - day of the month, 1 to 31
  • [tm_mon] - month of the year, 0 (Jan) to 11 (Dec)
  • [tm_year] - years since 1900
  • [tm_wday] - day of the week, 0 (Sun) to 6 (Sat)
  • [tm_yday] - day of the year, 0 to 365
  • [tm_isdst] - is daylight savings time in effect? Positive if yes, 0 if not, negative if unknown.

Exceptions

If the time zone is not valid, generates a E_WARNING.

Example: localtime() example

The example below shows the usage of localtime() function.

<?php
//displaying the local time as
//a numerically indexed array
echo ("The local time is :\n");
print_r(localtime());
?>

The output of the above code will be:

The local time is :
Array
(
    [0] => 31
    [1] => 6
    [2] => 7
    [3] => 30
    [4] => 8
    [5] => 121
    [6] => 4
    [7] => 272
    [8] => 0
)

Example: using associative parameter

When associative parameter is set to true, the function returns an associative array as shown in the example below:

<?php
//displaying the local time as
//an associative array
echo ("The local time is :\n");
print_r(localtime(time(), true));
?>

The output of the above code will be:

The local time is :
Array
(
    [tm_sec] => 28
    [tm_min] => 9
    [tm_hour] => 7
    [tm_mday] => 30
    [tm_mon] => 8
    [tm_year] => 121
    [tm_wday] => 4
    [tm_yday] => 272
    [tm_isdst] => 0
)

❮ PHP Date and Time Reference