Skip to main content

How to Get the Current Unix Time in PHP with time()

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Unix time counts the seconds since 1 January 1970 UTC, and many systems store and exchange times that way. In PHP, time() returns it as a whole number, and gmdate() turns it into readable text in UTC.

Syntax​

Syntax
$<now> = time();
echo gmdate(<format>, $<now>);
PartWhat it does
time()The current Unix time, in whole seconds.
gmdate(<format>, <timestamp>)Formats a timestamp as text, in UTC.
DATE_ATOMA built-in format for ISO 8601 dates, such as 2026-11-26T07:38:08+00:00.

The PHP manual for time(): "Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)." And for gmdate(): "Identical to the date() function except that the time returned is Greenwich Mean Time (GMT)."

The Example​

The example prints the current Unix time and the same moment as a date and time in UTC.

Prerequisites​

  • PHP for the command line. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which stores time() in $now, echoes it, and echoes gmdate(DATE_ATOM, $now).

main.php
<?php

$now = time();

echo $now, "\n";
echo gmdate(DATE_ATOM, $now), "\n";

Step 2: Run It​

Terminal
php main.php
Output
1795678688
2026-11-26T07:38:08+00:00

A zsh terminal where php main.php prints 1795678688 and 2026-11-26T07:38:08+00:00.

Your numbers will differ: they're the moment you run it. +00:00 shows the date is in UTC. The command is the same in zsh, bash and PowerShell.

Times in the Future​

Because it's a plain number of seconds, adding time is arithmetic: time() + 15 * 60 is the Unix time 15 minutes from now.

Common Mistakes​

MistakeWhat happensDo this instead
Using date() and expecting UTCdate() uses the configured default time zoneUse gmdate() for UTC
Treating time() as millisecondsIt's seconds; JavaScript's Date.now() is millisecondsMultiply or divide by 1,000 when you exchange values
Calling time() twice and expecting the same valueA second can pass between the callsCall it once and reuse the result

Troubleshooting​

SymptomLikely causeFix
A formatted time is hours offdate() used a local time zoneSwitch to gmdate(), or set the time zone explicitly
A date far in the futureMilliseconds were treated as secondsDivide by 1,000

FAQ​

Do Unix timestamps contain a time zone?​

No. The manual notes that "Unix timestamps do not contain any information with regards to any local timezone." The zone only matters when you format them.

How do I get fractions of a second?​

microtime(true) returns the current Unix time as a float with microseconds.

Conclusion​

time() returns the current Unix time in seconds, arithmetic moves it forward or back, and gmdate() formats it in UTC.

Additional Resources​