Overview
PHP has first-class support for Unix timestamps through time(), DateTimeImmutable, and timezone-aware formatting. The safest pattern is to create an immutable datetime from the timestamp and set the timezone explicitly before display.
This guide focuses on practical implementation choices: how values should be stored, how they should cross API boundaries, and where developers most often introduce timezone or precision bugs. The safest pattern is to keep the stored value unambiguous, document the unit, and format the date only when it reaches a human-facing interface.
When to use it
Use PHP timestamp conversion in Laravel apps, server-rendered pages, logs, cache expiry, job scheduling, and backend API normalization.
For production systems, also consider how the value will be indexed, logged, serialized, and read by other teams. A timestamp field that is obvious in one programming language can become ambiguous when it is consumed by JavaScript, SQL, mobile clients, or third-party integrations.
Developer examples
Use examples like this as a starting point, then adapt the timezone and precision to your application contract.
$timestamp = time();
$date = (new DateTimeImmutable('@'.$timestamp))->setTimezone(new DateTimeZone('UTC'));
echo $date->format(DateTimeInterface::ATOM);
Common pitfalls
PHP default timezone settings affect formatted output. Set the timezone intentionally when converting for users or APIs.
Most timestamp bugs come from hidden assumptions: local time treated as UTC, seconds treated as milliseconds, formatted strings parsed without offsets, or narrow integer columns copied from old examples. Add tests for boundary dates and document the expected unit beside every external timestamp field.
FAQ
What does time() return?
time() returns the current Unix timestamp in seconds.
How do I parse a timestamp in PHP?
Use DateTimeImmutable with the @ prefix, then set the desired timezone.