PHP time() Function

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

Definition

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();
CallWhat it does
time()Returns the current Unix timestamp as an integer.
time() + 3600Returns 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

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
intThe 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.

PHP
<?php
date_default_timezone_set('UTC');

$now = time();

var_dump(is_int($now));
var_dump($now > strtotime('2020-01-01 00:00:00'));
var_dump((int) date('Y', $now) >= 2020);

How It Works

  1. The time zone is set to UTC so the fixed date used for comparison is always the same.
  2. time() is stored in $now so every line below uses the same moment.
  3. is_int() confirms that the timestamp is a whole number.
  4. strtotime() turns a fixed date into a timestamp, and the current time is always larger than that date.
  5. date('Y', $now) gives the current year, which is certainly 2020 or later.

Example 2 — Adding Seconds to Build an Expiry Time

Because a timestamp is a plain number, you can add seconds to it. This is a common way to build an expiry time for a login session or a cache entry.

PHP
<?php
$now = time();

$expires = $now + 3600;

echo "Seconds to wait: ", $expires - $now, PHP_EOL;
echo "Minutes to wait: ", ($expires - $now) / 60, PHP_EOL;
var_dump($expires > $now);

How It Works

  1. The current time is saved once in $now.
  2. Adding 3600 seconds (60 x 60) gives a timestamp exactly one hour later.
  3. Subtracting the two timestamps gives the length of the wait, which is always 3600.
  4. Dividing by 60 turns the seconds into minutes.
  5. The expiry time is greater than the current time, so the comparison is true.

Example 3 — The Unix Epoch and Measuring Elapsed Time

A timestamp of 0 is the very start of the Unix epoch. This example shows that, and also how to measure how many seconds have passed between two calls.

PHP
<?php
echo strtotime('1970-01-01 00:00:00 UTC'), PHP_EOL;
echo strtotime('1970-01-02 00:00:00 UTC'), PHP_EOL;

$start = time();
$elapsed = time() - $start;

var_dump($elapsed >= 0);
var_dump(time() >= $start);

How It Works

  1. January 1, 1970 at 00:00:00 UTC is the epoch, so its timestamp is 0.
  2. One day later the timestamp is 86400, because a day has 24 x 60 x 60 seconds.
  3. $start remembers the moment the script began measuring.
  4. Subtracting $start from a later call to time() gives the elapsed seconds, which is never negative.
  5. 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.

Next: timezone_abbreviations_list()

timezone_abbreviations_list() returns a big array that describes the time zone abbreviatio.

timezone_abbreviations_list() 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