Computer >> Computer tutorials >  >> Programming >> PHP

localtime() function in PHP


The localtime() function returns an array that contains the time components of a Unix timestamp.

Syntax

localtime(timestamp, is_associative)

Parameters

  • timestamp − an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

  • is_associative − If set to FALSE or not supplied than the array is returned as a regular, numerically indexed array. If the argument is set to TRUE then localtime() is an associative array containing all the different elements of the structure returned by the C function call to localtime.

  • The names of the different keys of the associative array are as follows −

    • [tm_sec] − seconds

    • [tm_min] − minutes

    • [tm_hour] − hour

    • [tm_mday] − day of the month

    • [tm_mon] − month of the year (January=0)

    • [tm_year] − Years since 1900

    • [tm_wday] − Day of the week (Sunday=0)

    • [tm_yday] − Day of the year

    • [tm_isdst] − Is daylight savings time in effect

Return

The localtime() function returns an array that contains the time components of a Unix timestamp.

The following is an example −

Example

<?php
$localtime = localtime();
$localtime_assoc = localtime(time(), true);
print_r($localtime);
print_r($localtime_assoc);
?>

The following is the output −

Output

Array
(
   [0] => 52
   [1] => 14
   [2] => 5
   [3] => 11
   [4] => 9
   [5] => 118
   [6] => 4
   [7] => 283
   [8] => 0
)
Array
(
   [tm_sec] => 52
   [tm_min] => 14
   [tm_hour] => 5
   [tm_mday] => 11
   [tm_mon] => 9
   [tm_year] => 118
   [tm_wday] => 4
   [tm_yday] => 283
   [tm_isdst] => 0
)

Let us see another example −

Example

<?php
echo ("The local time is : \n");
print_r(localtime());
?>

The following is the output −

Output

The local time is :
Array
(
   [0] => 35
   [1] => 15
   [2] => 5
   [3] => 11
   [4] => 9
   [5] => 118
   [6] => 4
   [7] => 283
   [8] => 0
)