'~12 second time difference from timestamp

I'm trying to make a timer and display the remaining time on the page.

Time when the timer started:

$timeZone = new DateTimeZone('Europe/Riga');
$now = new DateTimeImmutable('now', $timeZone);

And save to database:

$entity->setTimer($now);

$this->em->persist($entity);
$this->em->flush();

I take timestamp:

$dateTime = $entity->getTimer();
$dateTime->getTimestamp();

Further on FE:

const currentD = new Date();
const startD = new Date(timestamp * 1000);
const timer = 5; // Timer in minutes
let endD = new Date(timestamp * 1000);

endD.setMinutes(startD.getMinutes() + timer);

const remainingTimeMinutes = Math.floor((endD - currentD) / 60000);
const remainingTimeSeconds = (((endD - currentD) % 60000) / 1000).toFixed(0);

And for some reason startD is ~12 seconds longer than currentD. Why is this happening?

Fri May 20 2022 10:05:04 GMT+0300 // currentD

Fri May 20 2022 10:05:13 GMT+0300 // startD



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source