Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Date and Time

PHP time() function

Reference for PHP time(): get the current Unix timestamp in seconds, how it relates to time zones, how to do arithmetic with it and what to use instead.

The time() function returns the current moment as a Unix timestamp: the number of whole seconds since 00:00:00 UTC on 1 January 1970 (the Unix epoch). A timestamp is a plain integer, so it is easy to store, compare and subtract, and it does not depend on any time zone.

Time zones only matter when you turn a timestamp into text, with date() or a DateTime object. For sub-second precision use microtime() instead.

PHP
<?php
$now = time();
echo $now + 3600;   // one hour from now

Try it

PHP Timestamps as numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Measuring and expiring

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
time(): int

This function takes no parameters.

Return value: the current Unix timestamp as an int (seconds since the epoch, in UTC). It does not throw.

Usage notes

  • Always UTC. time() is the same number everywhere on Earth at the same instant. The default time zone only changes how date() prints it.
  • Not a clock for measuring durations. If the system clock is adjusted, time() jumps. Use hrtime() for elapsed time.
  • Whole seconds only. Two calls in the same second return the same value. Use microtime(true) for a float with microseconds.
  • Do not add 86400 to mean "tomorrow" when daylight saving time matters. A local day can be 23 or 25 hours long; use DateTimeImmutable::modify("+1 day"), which respects the time zone.
  • Testing is hard. Code that calls time() directly is difficult to test. Pass the current time in as an argument, or inject a clock.
  • The 2038 problem only affects 32-bit integers. On 64-bit PHP, time() stays valid for billions of years.
  • $_SERVER["REQUEST_TIME"] holds the time the request started; REQUEST_TIME_FLOAT has microseconds.