Unix Timestamp in PHP

PHP's time() returns the current Unix timestamp in seconds, and gmdate('c', $ts) formats a timestamp as an ISO 8601 string in UTC. DateTime::createFromFormat('U', $ts) builds an object.

Get & convert epoch time in PHP

<?php
echo gmdate("c", 1700000000); // 2023-11-14T22:13:20.000Z
echo time(); // current epoch
Gotcha: date() uses the default timezone set by date_default_timezone_set(); use gmdate() when you specifically want UTC.

Frequently asked questions

How do I get the current Unix timestamp in PHP?
time() returns the current time in seconds.
How do I convert a timestamp to a date in PHP?
gmdate('Y-m-d H:i:s', $ts) for UTC, or date(...) for the configured local zone.

Other languages

Copied