How to Convert Unix Timestamps in JavaScript, Python, PHP, and SQL

If you work with APIs long enough, you'll eventually see a response like this:

{
  "created_at": 1717000000
}

At first glance, it doesn't tell you much. Is it a date? A record ID? Some kind of hash?

Most developers recognize it as a Unix timestamp, but that's usually where the easy part ends. The real challenge begins when you need to convert it into a readable date, send it across services, store it in a database, or display it correctly in a UI.

I've lost count of how many debugging sessions started with a timestamp that "looked wrong." In most cases, the timestamp was perfectly valid. The problem was how it was being converted.

This guide walks through practical timestamp conversion in JavaScript, Python, PHP, MySQL, and PostgreSQL, including the pitfalls that cause the most production issues.


What Is a Unix Timestamp?

A Unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC (the Unix Epoch). The value 1717000000 represents a specific moment in May 2024.

Unlike formatted dates, timestamps are timezone neutral, easy to compare, compact to store, and consistent across languages. If you're new to the concept, start with the complete guide to Unix timestamps before diving into conversion code.


Why Timestamp Conversion Causes Problems

The conversion itself isn't hard. The challenge is that different languages use different units.

Some expect seconds (Python, PHP, MySQL). Others expect milliseconds (JavaScript, Java). A perfectly valid timestamp produces a completely incorrect date if the wrong unit is used.

Consider 1717000000 (seconds) vs 1717000000000 (milliseconds). The difference is only three zeros. The resulting date can differ by decades. If this is a recurring issue in your codebase, the seconds vs milliseconds guide covers it in detail.


Converting Unix Timestamps in JavaScript

JavaScript is the biggest offender in this space because Date uses milliseconds while most APIs return seconds.

Timestamp to Date

A common mistake:

new Date(1717000000);
// → Tue Jan 20 1970 ...  ← wrong

JavaScript expects milliseconds. The correct conversion:

const date = new Date(1717000000 * 1000);
console.log(date);
// → Wed May 29 2024 16:53:20 GMT+0000

That * 1000 is not optional. Forget it and you're debugging a date from 1970.

Date to Timestamp

// Current time in seconds
Math.floor(Date.now() / 1000);
// → 1717000000

// Current time in milliseconds
Date.now();
// → 1717000000000

ISO String to Timestamp

const timestamp = Math.floor(
  new Date("2025-01-01T00:00:00Z").getTime() / 1000
);
console.log(timestamp);
// → 1735689600

Always divide by 1000 and floor the result. JavaScript's getTime() returns milliseconds, so leaving it unconverted produces a value that won't match backend expectations.


Converting Unix Timestamps in Python

Python's datetime module makes timestamp handling clean, and it uses seconds by default, which aligns with most backend systems.

Timestamp to Datetime

from datetime import datetime

dt = datetime.fromtimestamp(1717000000)
print(dt)
# → 2024-05-29 16:53:20

For UTC explicitly:

from datetime import datetime, timezone

dt = datetime.fromtimestamp(1717000000, tz=timezone.utc)
print(dt)
# → 2024-05-29 16:53:20+00:00

Datetime to Timestamp

from datetime import datetime

dt = datetime(2025, 1, 1)
timestamp = int(dt.timestamp())
print(timestamp)
# → 1735689600

Current Unix Timestamp

import time

print(int(time.time()))
# → 1717000000

Python returns a float with sub-second precision from time.time(). Casting to int() gives you standard Unix seconds.


Converting Unix Timestamps in PHP

PHP has supported Unix timestamps since its earliest days because the language grew up alongside web development.

Current Timestamp

echo time();
// → 1717000000

Timestamp to Date

echo date('Y-m-d H:i:s', 1717000000);
// → 2024-05-29 16:53:20

For UTC specifically:

echo gmdate('Y-m-d H:i:s', 1717000000);
// → 2024-05-29 16:53:20

Date to Timestamp

echo strtotime('2025-01-01');
// → 1735689600

strtotime() is remarkably flexible — it parses natural language dates like next Monday or +1 week. Powerful, but be careful with ambiguous formats like 01/02/2025 which depends on server locale settings.


Converting Unix Timestamps in MySQL

Most backend systems eventually need timestamp conversion at the database layer.

Current Timestamp

SELECT UNIX_TIMESTAMP();
-- → 1717000000

Timestamp to Datetime

SELECT FROM_UNIXTIME(1717000000);
-- → 2024-05-29 16:53:20

Datetime to Timestamp

SELECT UNIX_TIMESTAMP('2025-01-01 00:00:00');
-- → 1735689600

Filtering by Time Range

SELECT *
FROM orders
WHERE created_at > UNIX_TIMESTAMP() - 86400;

This retrieves records from the last 24 hours (86,400 seconds). Using integer comparisons is fast and index-friendly.


Converting Unix Timestamps in PostgreSQL

PostgreSQL uses different function names but the same concepts.

Current Timestamp

SELECT EXTRACT(EPOCH FROM NOW());
-- → 1717000000.123456

PostgreSQL returns fractional seconds by default. Cast to integer if you need standard 10-digit Unix seconds:

SELECT FLOOR(EXTRACT(EPOCH FROM NOW()))::BIGINT;

Timestamp to Date

SELECT TO_TIMESTAMP(1717000000);
-- → 2024-05-29 16:53:20+00

Date to Timestamp

SELECT EXTRACT(EPOCH FROM TIMESTAMP '2025-01-01 00:00:00');
-- → 1735689600

Handling Millisecond Timestamps

Many modern APIs return 13-digit values (1717000000000) instead of 10-digit values (1717000000). The fastest way to detect the format:

const isMilliseconds = timestamp.toString().length === 13;
len(str(timestamp)) == 13

As a rule: 10 digits = seconds, 13 digits = milliseconds. When you're unsure, paste the value into a timestamp converter rather than guessing.


Working with Timezones During Conversion

A common misconception: timestamps contain timezone information. They don't. A timestamp represents a moment in UTC. Timezone conversion happens at display time.

The value 1735689600 displays as 2025-01-01 00:00 UTC, 2024-12-31 19:00 EST, or 2025-01-01 09:00 JST. The timestamp is identical. Only the presentation changes.

If you need a deeper understanding of how timezones interact with timestamps, read why timezones break applications.


Best Practices for Timestamp Conversion

Store UTC Everywhere

Store timestamps in UTC. Convert to local time only when displaying information to users. This eliminates entire categories of timezone bugs.

Document Timestamp Units

Instead of {"created_at": 1717000000}, document created_at: Unix timestamp (seconds). Three words prevent misunderstandings between frontend and backend teams.

Validate Incoming Data

Before conversion, normalize:

function toSeconds(timestamp) {
  return timestamp.toString().length >= 13
    ? Math.floor(timestamp / 1000)
    : timestamp;
}

A one-line validation prevents hours of debugging.

Use Conversion Tools During Debugging

When 1717000000 shows up in a log or API response, convert it immediately rather than guessing what it represents. The DevFormatters Timestamp Converter is faster than writing temporary code snippets.


Frequently Asked Questions

How do I convert a Unix timestamp to a date?

Each language has its own approach:

  • JavaScript: new Date(timestamp * 1000)
  • Python: datetime.fromtimestamp(timestamp)
  • PHP: date('Y-m-d H:i:s', $timestamp)
  • MySQL: FROM_UNIXTIME(timestamp)
  • PostgreSQL: TO_TIMESTAMP(timestamp)

Why does JavaScript require multiplying by 1000?

JavaScript Date objects use milliseconds. Most Unix timestamps use seconds. The multiplication converts between the two.

How do I get the current Unix timestamp?

  • JavaScript: Math.floor(Date.now() / 1000)
  • Python: int(time.time())
  • PHP: time()
  • MySQL: SELECT UNIX_TIMESTAMP()
  • PostgreSQL: SELECT EXTRACT(EPOCH FROM NOW())

How do I know if a timestamp is seconds or milliseconds?

Count the digits. 10 digits = seconds. 13 digits = milliseconds.

Are Unix timestamps always UTC?

Yes. Unix timestamps represent UTC-based moments in time. Timezone conversion happens when displaying the value.

Should APIs return timestamps or ISO dates?

Both approaches are valid. Many systems store timestamps internally and expose ISO 8601 dates externally for readability. The important thing is consistency and clear documentation.


Related Resources


Timestamp conversion looks simple until it becomes part of a distributed system. A frontend in JavaScript, a backend in Python, a MySQL database, and a third-party API may all represent time differently. Most timestamp bugs aren't caused by incorrect values — they're caused by incorrect assumptions.

Once you understand how each language handles timestamps, conversions become predictable. When you're unsure whether a value represents seconds, milliseconds, UTC, or local time, verify it immediately. A few seconds checking a timestamp is cheaper than hours debugging a production incident.

If you work with APIs, JWT tokens, databases, or analytics systems, the DevFormatters Timestamp Converter lets you convert between Unix timestamps, milliseconds, UTC dates, and human-readable formats without switching between terminal windows, browser consoles, and throwaway scripts.