PHP | gettimeofday() Function

The gettimeofday() function is an inbuilt function in PHP which is used to return the current time. It is an interface to Unix system call gettimeofday(2). It returns an associative array containing the data returned from the system call. The float option is sent as a parameter to the gettimeofday() function and returns an associative array containing the current time.

Syntax:

gettimeofday( $return_float )

Parameters: This function accepts single parameter $return_float which is optional. This parameter is used to set to TRUE, then it returns a float value instead of an array.

Return Value: It returns an associative array containing the current time. If $return_float parameter is set then it return float value.
The associative array consists of the following array keys:



  • sec: It is used to specify the seconds since the Unix Epoch.
  • usec: It is used to specify the microseconds.
  • minuteswest: It is used to specify the minutes west of Greenwich.
  • dsttime: It is used to specify the type of dst correction.

Exception: The $return_float parameter was added since PHP 5.1.0 version.

Below programs illustrate the gettimeofday() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Displaying the current time
// as an associative array
echo ("Current time is: ");
print_r(gettimeofday());
?>

chevron_right


Output:

Current time is: Array
(
    [sec] => 1536040360
    [usec] => 178383
    [minuteswest] => 0
    [dsttime] => 0
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Displaying the current time
// as a float value
echo ("Current time is: ");
print_r(gettimeofday(true));
?>

chevron_right


Output:

Current time is: 1536040361.1613

Related Articles:

Reference: http://php.net/manual/en/function.gettimeofday.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.