PHP | localtime() Function

The localtime() function is an inbuilt function in PHP which is used to return the local time. The array returned by the localtime() function is similar to the structure returned by the C function call. The $timestamp and $is_associative are sent as parameters to the localtime() function and it returns an array that contains the components of a Unix timestamp.

Syntax:

array localtime( $timestamp, $is_associative )

Parameters: This function accepts two parameters as mentioned above and described below.

  • $timestamp: It is an optional parameter which specifies the Unix timestamp. Its default value is current local time.
  • $is_associative: It is an optional parameter which specifies whether to return an associative or indexed array. The value of associative array are:
    • 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.

Return Value: This function returns an array that contains the components of a Unix timestamp.

Exceptions:

  • The localtime() function generates a E_NOTICE if the time zone specified is not valid.
  • The localtime() function generates a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable

Below programs illustrate the localtime() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

The local time is :Array
(
    [0] => 22
    [1] => 24
    [2] => 10
    [3] => 28
    [4] => 7
    [5] => 118
    [6] => 2
    [7] => 239
    [8] => 0
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

The local time is :Array
(
    [tm_sec] => 23
    [tm_min] => 24
    [tm_hour] => 10
    [tm_mday] => 28
    [tm_mon] => 7
    [tm_year] => 118
    [tm_wday] => 2
    [tm_yday] => 239
    [tm_isdst] => 0
)

Related Articles:

Reference: http://php.net/manual/en/function.localtime.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.