<?php
function createDtFromTimestamp(float $timestamp): \DateTime
{
$timestamp = round($timestamp * 1e6) * 1e-6;
$sec = (int) sprintf('%0.0f', floor($timestamp));
$usec = (int) sprintf('%0.0f', min(max(($timestamp - $sec) * 1e6, 0), 999999));
$datetime = new \DateTime(date('Y-m-d H:i:s.', $sec) . sprintf('%06d', $usec));
return $datetime;
}
function dumpDt(\DateTime $dt) {
echo $dt->format('Y-m-d H:i:s.u') . ' ' . $dt->getTimestamp() . ' ' . $dt->format('u') . "\n";
}
foreach ([0.4, 0, -0.4, -1, -1.4] as $ts) {
$dateExpected = createDtFromTimestamp($ts);
dumpDt($dateExpected);
$date = new DateTime('@' . $ts);
dumpDt($date);
echo "\n";
}
preferences:
109.9 ms | 409 KiB | 5 Q