Sunday, November 17, 2024
Google search engine
HomeLanguagesPHP | gettimeofday() Function

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:




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


Output:

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

Program 2:




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


Output:

Current time is: 1536040361.1613

Related Articles:

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

RELATED ARTICLES

Most Popular

Recent Comments