How to get the current Unix timestamp in code

By TechCompare · Updated · How we check

In five of these six snippets the main time call returns seconds, but JavaScript's Date.now() returns milliseconds, so check the unit before you copy a line.

Copy these lines to get the current Unix timestamp in seconds or milliseconds. Each block also turns the example moment 1780511400 back into a date, which is June 3, 2026 at 18:30:00 UTC, so you have a known value to check your output against.

Unix timestamp
1,780,511,400
Seconds since epoch
ISO 8601
2026-06-03T18:30:00.000Z
Wed, 03 Jun 2026 18:30:00 GMT
Category
Code Examples
Timestamp knowledge base

How this is calculated

JavaScript counts in milliseconds, and Date.now() returns them directly. Divide by 1000 and round down with Math.floor for seconds, and pass the millisecond form of the example to new Date().

JavaScript
const seconds = Math.floor(Date.now() / 1000) // seconds
const milliseconds = Date.now() // milliseconds
const iso = new Date(1780511400000).toISOString() // 2026-06-03T18:30:00.000Z

Python counts in seconds, and time.time() returns them as a float, so int() gives whole seconds. For milliseconds, use time.time_ns() // 1_000_000, and always pass tz=timezone.utc to fromtimestamp(), because without it you get a naive datetime in the platform's local time.

Python
import time
from datetime import datetime, timezone
seconds = int(time.time())  # seconds
milliseconds = time.time_ns() // 1_000_000  # milliseconds
date = datetime.fromtimestamp(1780511400, tz=timezone.utc)  # June 3, 2026 at 18:30:00 UTC

PHP counts in seconds, and time() returns them directly. microtime(true) returns the current time as a float in seconds, so multiplying by 1000 and casting to int gives milliseconds.

PHP
<?php
$seconds = time(); // seconds
$milliseconds = (int) (microtime(true) * 1000); // milliseconds
$date = gmdate('Y-m-d H:i:s', 1780511400); // June 3, 2026 at 18:30:00 UTC

Go counts in seconds with Unix() and in milliseconds with UnixMilli() (Time.Unix, Time.UnixMilli). Skip UnixNano unless you need nanoseconds, because its result is undefined for dates before the year 1678 or after 2262.

Go
now := time.Now()
seconds := now.Unix() // seconds
milliseconds := now.UnixMilli() // milliseconds
date := time.Unix(1780511400, 0).UTC().Format(time.RFC3339) // 2026-06-03T18:30:00Z

PostgreSQL counts in seconds. EXTRACT returns numeric from version 14 on, with fractions of a second, so FLOOR and a bigint cast give whole seconds. to_timestamp() goes the other way and returns a timestamp with time zone. Don't feed it an epoch taken from a plain timestamp column, because PostgreSQL warns that the result assumes the original value was in UTC.

PostgreSQL
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()))::bigint; -- seconds
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::bigint; -- milliseconds
SELECT to_timestamp(1780511400) AT TIME ZONE 'UTC'; -- June 3, 2026 at 18:30:00 UTC

MySQL counts in seconds from UNIX_TIMESTAMP(), which returns 0 for a date outside its range, and that range ends in 2038 on 32-bit platforms and goes past the year 3000 on 64-bit ones. FROM_UNIXTIME() shows its result in the session time zone, so the output changes with the connection's setting.

MySQL
SELECT UNIX_TIMESTAMP(); -- seconds
SELECT ROUND(UNIX_TIMESTAMP(NOW(3)) * 1000); -- milliseconds
SELECT FROM_UNIXTIME(1780511400); -- June 3, 2026 at 18:30:00 when the session uses UTC

Store the value as a UTC integer or in a native timestamp column, and convert it at the edge, where a person reads it. Put the unit in the field name too, like created_at_ms, so nobody has to guess what the number counts.

Verdict

Store seconds unless a platform asks for milliseconds, and convert only at that boundary. Add one unit test that passes 1780511400 into your conversion and checks the result against the example date above. Don't format local times into strings and store those, because the string can't be turned back into the same instant without guessing the zone.

More Timestamp scenarios

Frequently asked questions

How do I get the Unix timestamp in milliseconds in Python?
Use time.time_ns() // 1_000_000. The time.time_ns() call returns nanoseconds as an integer, and floor division by 1_000_000 leaves whole milliseconds. See Python's time module docs for the call.
Why does MySQL FROM_UNIXTIME show a different time than UTC?
Because FROM_UNIXTIME() returns the value in the session time zone, not UTC. When the session is set to another zone, the same number prints a different wall clock time. Check the connection's time zone setting, or convert the value in your application code.
Does PostgreSQL EXTRACT(EPOCH) return a decimal?
Yes. From PostgreSQL 14 on, EXTRACT returns numeric, and the value keeps fractions of a second (release notes). Wrap it in FLOOR and cast to bigint when you want whole seconds.
How do I get a Unix timestamp in PHP with milliseconds?
Use (int) (microtime(true) * 1000). microtime(true) returns the time in seconds as a float, so multiplying by 1000 and casting to int gives whole milliseconds.
Is Date.now() in seconds or milliseconds?
Milliseconds. MDN defines it that way. For seconds, use Math.floor(Date.now() / 1000), and see the seconds vs milliseconds page to tell the two apart in stored data.