Open In App

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.

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



Exceptions:

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


Article Tags :