Unix Timestamps Explained: A Developer's Guide

Unix timestamps are one of the most common ways to represent time in programming. If you have ever seen a number like 1718460000 in a database or API response and wondered what it meant, this guide is for you.

What Is a Unix Timestamp?

A Unix timestamp (also called Epoch time or POSIX time) is simply the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC. This moment is known as the Unix Epoch.

For example:

  • 0 = January 1, 1970 00:00:00 UTC
  • 86400 = January 2, 1970 00:00:00 UTC (86,400 seconds = 1 day)
  • 1718460000 = June 15, 2024 14:00:00 UTC

Use our Epoch Converter to translate timestamps to human-readable dates and back.

Why Use Unix Timestamps?

Timestamps offer several advantages over date strings:

  • No timezone ambiguity - A timestamp always represents the same moment globally
  • Compact storage - A single integer takes less space than a formatted date string
  • Easy comparison - Comparing two times is just comparing two numbers
  • Simple arithmetic - Adding 3600 seconds always adds one hour
  • Universal support - Every programming language handles Unix time

Seconds vs Milliseconds

You will encounter two common formats:

  • Seconds (10 digits): 1718460000 - Traditional Unix format
  • Milliseconds (13 digits): 1718460000000 - Used by JavaScript

Converting between them is simple: divide or multiply by 1000.

Common Operations by Language

JavaScript

// Current timestamp (milliseconds)
Date.now()

// Current timestamp (seconds)
Math.floor(Date.now() / 1000)

// Timestamp to Date
new Date(1718460000 * 1000)

// Date to timestamp
Math.floor(new Date('2024-06-15').getTime() / 1000)

Python

import time
from datetime import datetime

# Current timestamp
time.time()

# Timestamp to datetime
datetime.fromtimestamp(1718460000)

# Datetime to timestamp
datetime(2024, 6, 15).timestamp()

PHP

// Current timestamp
time();

// Timestamp to date
date('Y-m-d H:i:s', 1718460000);

// Date to timestamp
strtotime('2024-06-15');

The Year 2038 Problem

32-bit systems that store timestamps as signed integers will overflow on January 19, 2038, at 03:14:07 UTC. The maximum value a 32-bit signed integer can hold is 2,147,483,647, which corresponds to that exact moment.

Modern systems use 64-bit integers, which will not overflow for billions of years. However, some legacy systems and databases still use 32-bit timestamps, so this remains a concern for long-running applications.

Negative Timestamps

Timestamps can be negative to represent dates before 1970. For example, -86400 represents December 31, 1969. This works in most modern systems but can cause issues with older software.

Best Practices

  • Store in UTC - Always store timestamps in UTC and convert to local time for display
  • Use milliseconds for JavaScript - JavaScript Date expects milliseconds
  • Be explicit about format - Document whether your API returns seconds or milliseconds
  • Consider ISO 8601 for APIs - 2024-06-15T14:00:00Z is more human-readable
  • Test around DST transitions - Ensure your code handles time zone changes correctly

Useful Tools

Bookmark these ClockTools utilities for working with timestamps: