PHP gettimeofday() Function (With Examples)

In this tutorial, we will learn about the PHP gettimeofday() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP gettimeofday() function

The gettimeofday() function is used to get the current time.


The syntax of the gettimeofday() function:



The parameters of the gettimeofday() function:

  • return_float – It is an optional parameter which is used to specify the return value as float instead of an associative array. To specify the float value – we set its value as "true".

Return Value

It returns an associative array with the following key values,

  • sec - It is used to get the time in seconds since the UNIX epoch
  • usec - It is used to get the time in microseconds
  • minuteswest - It is used to get the time in minutes (west of Greenwich)
  • dsttime - It is used for the type of dst correction

PHP gettimeofday() Function Example

PHP code to demonstrate an example of gettimeofday() function.

//getting the time with all keys
$result = gettimeofday();
echo "gettimeofday()...\n";

//extracting indivisual values based on the keys
echo "sec:          $result[sec]\n";
echo "usec:         $result[usec]\n";
echo "minuteswest:  $result[minuteswest]\n";
echo "dsttime:      $result[dsttime]\n";

//getting time in float
$result = gettimeofday(true);
echo "result in float: $result\n";


The output of the above example is:

    [sec] => 1565701750
    [usec] => 950047
    [minuteswest] => 0
    [dsttime] => 0
sec:          1565701750
usec:         950047
minuteswest:  0
dsttime:      0
result in float: 1565701751.0216

Reference: PHP gettimeofday() function

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.