PHP Unix Timestamp Guide: time(), DateTimeImmutable & Timezones
Passing a timezone object as the second argument to new DateTimeImmutable('@1764892800', new DateTimeZone('Asia/Tokyo')) silently ignores Tokyo time and forces UTC instead. That hidden PHP constructor rule breaks scheduled emails and invoice dates across thousands of web apps.
Get Current Seconds and Microseconds in PHP
PHP provides fast built-in functions for integer seconds and floating-point microseconds:
<?php
// Current Unix seconds (10 digits)
$seconds = time();
// Current time with microseconds as float
$microFloat = microtime(true);
// Integer milliseconds (13 digits)
$millis = (int) round(microtime(true) * 1000);
?>
Convert a Unix Timestamp Using DateTimeImmutable
Modern PHP code should prefer DateTimeImmutable over the older date() function or mutable DateTime. Prefixing the timestamp with @ always parses in UTC. As documented in the PHP DateTimeImmutable manual, you must call setTimezone() in a second step to convert into a local region.
<?php
$ts = 1764892800;
$utcDate = new DateTimeImmutable("@$ts");
$tokyoDate = $utcDate->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo $utcDate->format(DateTimeInterface::ATOM); // 2026-12-05T00:00:00+00:00
echo $tokyoDate->format('Y-m-d H:i:s T'); // 2026-12-05 09:00:00 JST
?>
Convert Human Dates Back to Unix Timestamps
Avoid using strtotime() for user-submitted forms because ambiguous dates like 05/06/2026 depend on separator rules. Use DateTimeImmutable::createFromFormat() with an explicit timezone instead:
<?php
$dt = DateTimeImmutable::createFromFormat(
'Y-m-d H:i:s',
'2026-12-05 14:30:00',
new DateTimeZone('UTC')
);
$unixTimestamp = $dt->getTimestamp();
echo $unixTimestamp;
?>
Check PHP 64-Bit Support for the Year 2038
On 32-bit PHP builds, getTimestamp() and strtotime() fail for dates after January 19, 2038. Verify that your production host runs 64-bit PHP by checking the PHP_INT_SIZE constant. If it equals 8, your server handles 64-bit timestamps safely. If it equals 4, upgrade your server or read our full Year 2038 Problem guide.