English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP localtime() Function Usage and Examples

PHP Date & Time Functions Manual

localtime() function

Definition and Usage

The localtime() function returns the local time as an array, with different parts of the time as elements of the array.

Syntax

localtime($timestamp, \t$is_assoc)

Parameters

Serial numberParameters and descriptions
1

timestamp(optional)

This is an integer value representing the Unix timestamp of the local time.

2

is_assoc(optional)

If set to FALSE or not provided, it returns a regular numeric indexed array. If the parameter is set to TRUE, the localtime() function returns an associative array containing all different units returned by the C localtime() function call. The different keys in the associative array are:

  • "tm_sec" - second number, 0 to 59

  • "tm_min" - minute number, 0 to 59

  • "tm_hour" - hour, 0 to 23

  • "tm_mday" - the day of the month, 1 to 31

  • "tm_mon" - the month of the year, 0 (Jan) to 11 (Dec)

  • "tm_year" - year, from 1900 starts

  • "tm_wday" - the day of the week, 0 (Sun) to 6 (Sat)

  • "tm_yday" - the day of the year, 0 to 365

  • "tm_isdst" - Is daylight saving time currently in effect? If it is in effect, it is a positive number. 0 represent not in effect, negative numbers represent unknown.

Return value

The PHP localtime() function returns an array representing the local time.

PHP version

This function was initially introduced in PHP version4introduced and can be used in all higher versions.

Online example

The following examples demonstratelocaltime()Function usage-

<?php
   $time = localtime();
   print_r($time);
?>
Test and see‹/›

Output result

Array
(
    [0] => 50
    [1] => 28
    [2] => 13
    [3] => 12
    [4] => 4
    [5] => 120
    [6] => 2
    [7] => 132
    [8] => 0
)

Online example

Now, let's pass the timestamp parameter.-

<?php
   $timestamp = time();
   $time = localtime($timestamp);
   print_r($time);
?>
Test and see‹/›

Output result

Array
(
    [0] => 21
    [1] => 54
    [2] => 13
    [3] => 12
    [4] => 4
    [5] => 120
    [6] => 2
    [7] => 132
    [8] => 0
)

Online example

If you go through-

<?php
   $timestamp1 = time() - (23*12*30);
   print_r($timestamp1); 
   print("\n");
   $timestamp2 = time() + (23*12*30);
   print_r($timestamp2); 
?>
Test and see‹/›

Output result

Normal array: Array
(
    [0] => 23
    [1] => 8
    [2] => 14
    [3] => 12
    [4] => 4
    [5] => 120
    [6] => 2
    [7] => 132
    [8] => 0
)
Associative array: Array
(
    [tm_sec] => 23
    [tm_min] => 8
    [tm_hour] => 14
    [tm_mday] => 12
    [tm_mon] => 4
    [tm_year] => 120
    [tm_wday] => 2
    [tm_yday] => 132
    [tm_isdst] => 0
)