In PHP, time() returns the current time as a Unix timestamp. That is the number of seconds that have passed since January 1, 1970 at 00:00:00 UTC.
A timestamp is just a whole number, which makes it easy to store, compare and do math with. You can add 3600 to move one hour ahead, subtract two timestamps to get a duration, or pass the number to date() to turn it into readable text.
Syntax
Call time() with the values you need. Start with this line.
PHP
$now = time();
Call
What it does
time()
Returns the current Unix timestamp as an integer.
time() + 3600
Returns the timestamp for exactly one hour from now.
date('Y-m-d', time())
Turns the current timestamp into a readable date string.
time() takes no arguments and always returns an int. The number is the same in every time zone, because a Unix timestamp is always counted from UTC. The time zone only matters when you format the number into text.
Library declarations
PHP
time(): int
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
int
The number of seconds since the Unix epoch (January 1, 1970 00:00:00 UTC).
PHP Examples
Example 1 — Checking the Type and Size of the Timestamp
The result of time() changes every second, so instead of printing it we check what kind of value it is and compare it with a fixed date.
January 1, 1970 at 00:00:00 UTC is the epoch, so its timestamp is 0.
One day later the timestamp is 86400, because a day has 24 x 60 x 60 seconds.
$start remembers the moment the script began measuring.
Subtracting $start from a later call to time() gives the elapsed seconds, which is never negative.
time() can only stay the same or grow, so it is never smaller than $start.
Frequently Asked Questions
It returns the current Unix timestamp as an integer. This is the number of seconds that have passed since January 1, 1970 at 00:00:00 UTC.
No. A Unix timestamp is always counted from UTC, so time() gives the same number everywhere in the world. The time zone only changes how the number looks when you format it with date() or DateTime.
Pass the timestamp to date(), for example date('Y-m-d', time()). If you leave out the second argument, date() uses the current time by itself, so date('Y-m-d') works too.
time() returns whole seconds as an int. microtime(true) returns the time as a float with microsecond precision, which is better for measuring how long a small piece of code takes.
Add 86400 seconds, like time() + 86400. This ignores daylight saving changes, so for calendar rules such as tomorrow at the same hour, use strtotime('+1 day') or DateTime::modify() instead.