From timestamp to UTC
The @ prefix creates a date from Unix seconds. DateTimeImmutable leaves the original object unchanged when its time zone is changed.
<?php
$date = new DateTimeImmutable('@1711972800');
echo $date->format('Y-m-d\TH:i:sP');
// 2024-04-01T12:00:00+00:00Display the time in Paris
$paris = $date->setTimezone(new DateTimeZone('Europe/Paris'));
echo $paris->format('Y-m-d\TH:i:sP');
// 2024-04-01T14:00:00+02:00From date to timestamp
$date = new DateTimeImmutable('2024-04-01T14:00:00+02:00');
echo $date->getTimestamp(); // 1711972800
// Current timestamp in seconds
echo time();Millisecond input
For this positive-value example, use a 64-bit integer and preserve the fraction with U.u. The u format expects six digits of microseconds.
$ms = 1711972800123;
$seconds = intdiv($ms, 1000);
$micros = ($ms % 1000) * 1000;
$date = DateTimeImmutable::createFromFormat(
'U.u', sprintf('%d.%06d', $seconds, $micros)
);
if ($date === false) {
throw new RuntimeException('Invalid date');
}
echo $date->format('Y-m-d\TH:i:s.uP');Validate input before converting it. getTimestamp() returns whole seconds and cannot preserve microseconds by itself. The division example above is intended only for positive timestamps.
References
PHP — compound formats · createFromFormat
English version published September 26, 2026.