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.

Syntax

The syntax of the gettimeofday() function:

gettimeofday(return_float);

Parameters

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.

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

//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";
?>

Output

The output of the above example is:

gettimeofday()...
Array
(
    [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:



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.