What is a Unix Timestamp?
A Unix timestamp (also known as Epoch time, POSIX time, or Unix Epoch time) is the number of seconds that have elapsed since January 1, 1970 at 00:00:00 UTC. This reference point is called the Unix Epoch.
For example, the timestamp 1704067200 represents January 1, 2024 at 00:00:00 UTC. Unix timestamps are timezone-independent, making them ideal for storing dates in databases and APIs.
Seconds vs Milliseconds
Unix timestamps can be expressed in two common formats:
- Seconds (10 digits): The traditional Unix format. Used by most server-side languages like PHP, Python, and Ruby. Example:
1704067200
- Milliseconds (13 digits): Provides more precision. Used by JavaScript's
Date.now() and Java. Example: 1704067200000
This converter auto-detects which format you're using based on the number of digits.
The Year 2038 Problem (Y2K38)
Systems using 32-bit signed integers to store Unix timestamps will overflow on January 19, 2038 at 03:14:07 UTC. At this moment, the timestamp reaches 2,147,483,647 (the maximum 32-bit signed integer), and incrementing it causes overflow.
Modern systems use 64-bit integers, extending the range to approximately 292 billion years in either direction — more than enough for any practical application.
Why Use Unix Timestamps?
- Timezone independence: A single number represents the same moment everywhere in the world.
- Easy comparison: Simply compare two numbers to determine which date is earlier or later.
- Storage efficiency: A single integer uses less space than date strings.
- No ambiguity: Avoids confusion from different date formats (MM/DD vs DD/MM).
- Easy arithmetic: Add or subtract seconds to calculate future or past dates.
Getting Current Timestamp in Code
Here's how to get the current Unix timestamp in various programming languages:
- JavaScript:
Math.floor(Date.now() / 1000) (seconds) or Date.now() (milliseconds)
- Python:
import time; int(time.time())
- PHP:
time()
- Java:
System.currentTimeMillis() / 1000
- Go:
time.Now().Unix()
- Ruby:
Time.now.to_i