English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP gettimeofday() Function Usage and Examples

PHP Date & Time Function Manual

The gettimeofday() function gets the current time

Definition and Usage

gettimeofday()This function returns the current time of the day. By default, this function returns the current time in array form. If the boolean valuetrueIf passed as a parameter, this function returns the time as a floating-point number.

Syntax

gettimeofday($return_float)

Parameter

NumberParameters and Description
1

return_float($Optional)

This is a boolean value that specifies whether the time should be a floating-point value. If the value is true, then this function returns the time as a floating-point value.

 The keys in the array are:

  • "sec" - Seconds since the Unix Epoch

  • "usec" - Microseconds

  • "minuteswest" - Minutes West of Greenwich

  • "dsttime" - Type of Daylight Saving Time Correction

Return Value

PHP gettimeofday()This function returns the current time. By default, this value will be an array containing the following keys: sec, usec, minuteswest, dsttime. If thereturn_floatIf the value is set to true, the time will be returned as a floating-point value.

PHP Version

This function was originally introduced in PHP version4introduced and can be used in all higher versions.

Online Example

The following example demonstratesgettimeofday()Function Usage-

<?php
   $time = gettimeofday();    
   print_r($time);   
?>
Test and see‹/›

Output Result

Array
(
    [sec] => 1589298247
    [usec] => 881165
    [minuteswest] => 0
    [dsttime] => 0
)

Online Example

The following example prints the current time as a floating-point number-

<?php
   $time = gettimeofday(true);    
   print_r($time); 
?>
Test and see‹/›

Output Result

1589298812.5101

Online Example

You can extract individual time values as follows-

<?php
   $time = gettimeofday();
   echo "sec: $time[sec]\n";
   echo "usec: $time[usec]\n";
   echo "minuteswest: $time[minuteswest]\n";
   echo "dsttime: $time[dsttime]"; 
?>
Test and see‹/›

Output Result

sec: 1589301022
usec: 843115
minuteswest: 0
dsttime: 0

Online Example

<?php
   print_r(gettimeofday());   
   echo gettimeofday(true);
?>
Test and see‹/›

This produces the following results-

Array
(
    [sec] => 1589261767
    [usec] => 31653
    [minuteswest] => 0
    [dsttime] => 0
)
1589261767.032