PHP gettimeofday() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, gettimeofday() returns the current time with microsecond precision, either as an array or as a float.

By default you get an array with the seconds, microseconds and time zone details. Pass true to get one float number instead, which is handy for simple timers.

Syntax

Call gettimeofday() with the values you need. Start with this line.

PHP
$now = gettimeofday();
CallWhat it does
gettimeofday()Returns an array with the keys sec, usec, minuteswest and dsttime.
gettimeofday(true)Returns the current time as a float, for example 1700000000.123456.
gettimeofday()['sec']Returns the whole seconds since the Unix epoch as an integer.

The values change on every call because this function reads the live clock. For that reason, the examples below print types and keys instead of the raw time. For timing code, microtime(true) and hrtime() do the same job.

Library declarations
PHP
gettimeofday(bool $as_float = false): array|float

Parameters

ParameterDescription
as_floatOptional. If true, a float with seconds and microseconds is returned. If false (the default), an array is returned.

Return Value

Return ValueDescription
arrayWhen as_float is false: keys sec (seconds), usec (microseconds), minuteswest (minutes west of UTC) and dsttime (daylight saving flag).
floatWhen as_float is true: the current Unix time including the fractional part.

PHP Examples

Example 1 — Array Keys and Zone Info

Call gettimeofday() with no argument and look at the structure of the result.

PHP
<?php
date_default_timezone_set('UTC');

$t = gettimeofday();

echo implode(', ', array_keys($t)), PHP_EOL;
echo $t['minuteswest'], PHP_EOL;

How It Works

  1. gettimeofday() returns an array because as_float is false by default.
  2. array_keys() shows the four keys: sec, usec, minuteswest and dsttime.
  3. UTC has no offset from itself, so minuteswest is 0.
  4. The sec and usec values are skipped because they change every call.

Example 2 — Return Type Depends on the Argument

The same function returns different types based on as_float.

PHP
<?php
$float = gettimeofday(true);
$array = gettimeofday();

echo gettype($float), PHP_EOL;
echo gettype($array), PHP_EOL;
echo gettype($array['sec']), PHP_EOL;

How It Works

  1. gettimeofday(true) returns a float. gettype() calls a float 'double'.
  2. gettimeofday() with no argument returns an array.
  3. Inside the array, sec is a whole number, so its type is integer.
  4. Always check how you call it before you do math on the result.

Example 3 — Find Your Offset from UTC

Use minuteswest to work out how far a time zone is from UTC.

PHP
<?php
date_default_timezone_set('Asia/Kolkata');

$t = gettimeofday();

echo 'Minutes west of UTC: ', $t['minuteswest'], PHP_EOL;
echo 'Hours east of UTC: ', -$t['minuteswest'] / 60, PHP_EOL;

How It Works

  1. We set the default time zone to Asia/Kolkata, which is UTC+5:30.
  2. minuteswest is negative because the zone is east of UTC: -330.
  3. Putting a minus sign in front flips it to 330.
  4. Dividing by 60 turns minutes into hours, which gives 5. 5.

Frequently Asked Questions

It returns the current time with microsecond precision. By default the result is an array, and with true it is a float.
Both read the live clock. gettimeofday() returns an array with time zone info, while microtime() returns a string like '0.5 1700000000' or a float. For simple timers, microtime(true) is more common.
It is the number of minutes the current time zone is west of UTC. Zones east of UTC, like India, give negative numbers.
Call gettimeofday(true). You get a float such as 1700000000.123456 that holds both seconds and microseconds.
Yes, subtract two gettimeofday(true) values. For better accuracy that cannot go backwards when the clock changes, use hrtime(true) instead.

Next: gmdate()

gmdate() formats a Unix timestamp as a date and time string in Greenwich Mean Time (GMT/UT.

gmdate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful