We now have a youtube channel. Subscribe!

PHP | localtime() Function

PHP localtime() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP localtime() Function.

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

Syntax

Following below is the syntax to use this function -

localtime($timestamp, $is_assoc)


Parameter Details

Sr.NoParameter & 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 Value

This built-in PHP function returns an array that represents the localtime.

PHP Version

This function was first introduced as part of the core PHP v 4 and, it works with all the later versions.

Example1

Following example illustrates the usage of the PHP localtime() function -

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

Output

When the above code is executed, it will produce the following result -

Array
(
    [0] => 22
    [1] => 22
    [2] => 15
    [3] => 22
    [4] => 10
    [5] => 120
    [6] => 0
    [7] => 326
    [8] => 0
)

Example2

Now let us invoke this built-in PHP function by passing all the needed parameters -

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

Output

When the above code is executed, it will produce the following result -

Array
(
    [0] => 41
    [1] => 26
    [2] => 15
    [3] => 22
    [4] => 10
    [5] => 120
    [6] => 0
    [7] => 326
    [8] => 0
)

Example3

Try the following example -

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

Output

When the above code is executed, it will produce the 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
)


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP microtime() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain