• PHP Video Tutorials

PHP localtime() Function


Advertisements

Definition and Usage

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

Syntax

localtime($timestamp, $is_assoc)

Parameters

Sr.No Parameter & Description
1

timestamp(Optional)

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

2

is_assoc(Optional)

This is a boolean value determining the type of the array returned. If this value is false the array returned will be normal array with integer index. If this value is true, the array returned will be associative with the components of a Unix time stamp as keys.

Keys of an associative array are: tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst

Return Values

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

PHP Version

This function was first introduced in PHP Version 4 and, works with all the later versions.

Example

Following example demonstrates the usage of the localtime() function −

Live Demo
<?php
   $time = localtime();
   print_r($time);
?>

This will produce following result −

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

Example

Now, let us try this function by passing the timestamp parameter .−

Live Demo
<?php
   $timestamp = time();
   $time = localtime($timestamp);
   print_r($time);
?>

This will produce following result −

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

Example

If you pass the −

<?php
   $timestamp1 = time() - (23*12*30);
   print_r($timestamp1); 
   print("\n");
   $timestamp2 = time() + (23*12*30);
   print_r($timestamp2); 
?>

This will produce following 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
)
php_function_reference.htm
Advertisements