Open In App

PHP | localtime() Function

Last Updated : 28 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


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:




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


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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads