Javascript timestamp parsed as wrong date by Carbon in Laravel

Viewed 738

I have the following code that stores the current timestamp in localStorage

localStorage.setItem('visitedOn', new Date().getTime())

which sets the timestamp to 1586525864525 (today a few minutes ago)

When I make an API request to my Laravel backend, and parse the timestamp using

Carbon::createFromTimestamp($request->visitedOn)

It returns the following value, which is completely off.

Carbon @1104613325 {#993
  date: 2005-01-01 22:02:05.0 Europe/Brussels (+01:00)
}

Any idea what might be going wrong? Are there multiple types of timestamps with different character lengths?

1 Answers

createFromTimestamp accepts a value in seconds and new Date().getTime() returns the time in milliseconds, so, you should use createFromTimestampMs that accepts the Unit timestamp in milliseconds.

Carbon::createFromTimestampMs($request->visitedOn)
Related