Every programming language handles unix timestamps slightly differently. Python gives you a float. JavaScript defaults to milliseconds. PHP has three different functions depending on what precision you need. I have burned time debugging timestamp bugs in all three — this guide covers every method so you get the right value the first time.
This is a practical code examples reference covering unix timestamp python, unix timestamp javascript, unix timestamp php, and every other major language. For quick conversion without code, use the Unix Timestamp Converter. For Discord-ready timestamp codes, use the Discord Timestamp Generator.
The One Rule Before Any Code
Discord requires seconds (10 digits) not milliseconds (13 digits). This applies everywhere. JavaScript Date.now() and getTime() return milliseconds by default. Always divide by 1000 before using any JavaScript timestamp in Discord or any system expecting standard unix time.
Unix Timestamp in Python
Get Current Timestamp
The simplest method uses the time module:
import time
print(time.time())
# 1782734488.123456 — float with microseconds
print(int(time.time()))
# 1782734488 — integer seconds
Using datetime Module (Recommended)
The datetime module is preferred for timezone.utc aware operations that avoid local timezone pitfalls:
from datetime import datetime, timezone
# Current timestamp as integer
current = int(datetime.now(timezone.utc).timestamp())
print(current)
# 1782734488
# From specific date
dt = datetime(2026, 6, 29, 12, 0, 0, tzinfo=timezone.utc)
print(int(dt.timestamp()))
# 1782734400
Convert DateTime to Unix Timestamp
datetime.timestamp() converts a datetime object to a POSIX timestamp — the number of seconds including fractions elapsed since January 1 1970 at 00:00:00 UTC:
from datetime import datetime, timezone
dt = datetime(2026, 6, 29, 12, 0, 0, tzinfo=timezone.utc)
timestamp = dt.timestamp()
print(timestamp)
# 1782734400.0
print(int(timestamp))
# 1782734400
Convert Unix Timestamp Back to Date
from datetime import datetime, timezone
timestamp = 1782734488
# UTC output
utc_date = datetime.fromtimestamp(timestamp, tz=timezone.utc)
print(utc_date.strftime('%Y-%m-%d %H:%M:%S'))
# "2026-06-29 12:01:28"
# Local timezone
local_date = datetime.fromtimestamp(timestamp)
print(local_date.strftime('%Y-%m-%d %H:%M:%S'))
Get Milliseconds in Python
from datetime import datetime, timezone
# Milliseconds — multiply by 1000
ms = int(datetime.now(timezone.utc).timestamp() * 1000)
print(ms)
# 1782734488000
Using time.mktime()
time.mktime() takes a timetuple() and converts to unix timestamp — note this is based on local timezone not UTC:
import time
from datetime import datetime
dt = datetime(2026, 6, 29, 12, 0, 0)
timestamp = time.mktime(dt.timetuple())
print(timestamp)
# Result varies by system timezone
Unix Timestamp in JavaScript
Get Current Timestamp
JavaScript Date.now() returns milliseconds — always divide by 1000 for seconds:
// Milliseconds (13 digits) — JavaScript default
console.log(Date.now());
// 1782734488000
// Seconds (10 digits) — standard unix timestamp
console.log(Math.floor(Date.now() / 1000));
// 1782734488
Using new Date()
// Current timestamp in seconds
const now = Math.floor(new Date() / 1000);
// Specific date in seconds — always use ISO 8601 with Z for UTC
const specific = Math.floor(new Date('2026-06-29T12:00:00Z').getTime() / 1000);
console.log(specific);
// 1782734400
Convert Unix Timestamp to Date
The Date object constructor expects milliseconds — multiply seconds by 1000:
const timestamp = 1782734488;
const date = new Date(timestamp * 1000);
console.log(date.toUTCString());
// "Mon, 29 Jun 2026 12:01:28 GMT"
console.log(date.toISOString());
// "2026-06-29T12:01:28.000Z"
console.log(date.toLocaleDateString());
// Varies by system locale settings
Get Individual Date Components
const date = new Date(1782734488 * 1000);
console.log(date.getFullYear()); // 2026
console.log(date.getMonth()); // 5 (0-indexed — June = 5)
console.log(date.getDate()); // 29
console.log(date.getHours()); // 12
console.log(date.getMinutes()); // 1
console.log(date.getSeconds()); // 28
Handle Both Seconds and Milliseconds Automatically
function toUnixSeconds(timestamp) {
const ts = String(timestamp);
if (ts.length === 13) {
return Math.floor(parseInt(ts, 10) / 1000);
}
return parseInt(ts, 10);
}
Date.now() and Reduced Time Precision
MDN notes that Date.now() precision may be rounded in browsers with fingerprinting protection enabled — particularly Firefox with privacy.resistFingerprinting. This deflects to 2ms precision. For performance API timing use performance.now() instead. For standard unix timestamp generation the rounding is negligible.
Unix Timestamp in PHP
Get Current Timestamp
PHP's time() function returns the current unix timestamp as an integer — seconds since January 1 1970 at 00:00:00 GMT. No parameters needed:
<?php
$timestamp = time();
echo $timestamp;
// 1782734488
Note from php.net: Unix timestamps do not contain any information about local timezone. Use DateTimeImmutable class for handling date and time information to avoid pitfalls that come with raw timestamps.
Three Ways to Get Current Timestamp in PHP
<?php
// Method 1 — time() — integer seconds (most common)
$t1 = time();
// Method 2 — microtime() — float seconds.microseconds
$t2 = microtime(true);
// Method 3 — request start time
$t3 = $_SERVER['REQUEST_TIME'];
Convert Date String to Unix Timestamp
strtotime() converts date string to unix timestamp. Always specify UTC to avoid setting timezone issues:
<?php
// From date string — specify UTC
$timestamp = strtotime('2026-06-29 12:00:00 UTC');
echo $timestamp;
// 1782734400
// strtotime('now') — current timestamp
$now = strtotime('now');
// Validate before using
if (is_numeric($timestamp) && $timestamp !== false) {
echo "Valid: " . $timestamp;
}
Convert Timestamp to Human Readable Date
<?php
$timestamp = 1782734488;
// Standard format
echo date('Y-m-d H:i:s', $timestamp);
// "2026-06-29 12:01:28"
// UTC output using gmdate()
echo gmdate('Y-m-d H:i:s', $timestamp);
// "2026-06-29 12:01:28" — always UTC
// Custom format
echo date('l, F j Y \a\t g:i A', $timestamp);
// "Monday, June 29 2026 at 12:01 PM"
Always Set Timezone in PHP
<?php
// Set at top of file or in php.ini
date_default_timezone_set('UTC');
$timestamp = time();
echo date('Y-m-d H:i:s', $timestamp);
Using DateTimeImmutable (Recommended)
<?php
// Get current timestamp
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
echo $now->getTimestamp();
// From existing timestamp
$dt = new DateTimeImmutable('@1782734488');
echo $dt->format('Y-m-d H:i:s');
// "2026-06-29 12:01:28"
Unix Timestamp in Other Languages
Java
// Current timestamp in seconds
long seconds = Instant.now().getEpochSecond();
// Current timestamp in milliseconds
long millis = System.currentTimeMillis();
// Specific date
long specific = Instant.parse("2026-06-29T12:00:00Z").getEpochSecond();
Go
import "time"
// Seconds
seconds := time.Now().Unix()
// Milliseconds
millis := time.Now().UnixMilli()
Ruby
# Seconds
Time.now.to_i
# Milliseconds
(Time.now.to_f * 1000).to_i
MySQL
-- Current timestamp
SELECT UNIX_TIMESTAMP();
-- Specific date
SELECT UNIX_TIMESTAMP('2026-06-29 12:00:00');
-- Convert back to date
SELECT FROM_UNIXTIME(1782734488);
PostgreSQL
-- Current timestamp
SELECT EXTRACT(EPOCH FROM now());
-- Convert back to date
SELECT TO_TIMESTAMP(1782734488);
Bash
# Current timestamp
date +%s
# Specific date
date -d '2026-06-29 12:00:00 UTC' +%s
Seconds vs Milliseconds — The Complete Reference
This table shows exactly what each language returns by default and how to get the other format:
| Language | Default | Get Seconds | Get Milliseconds |
|---|---|---|---|
Python time.time() | Float seconds | int(time.time()) | int(time.time() * 1000) |
JavaScript Date.now() | Milliseconds | Math.floor(Date.now() / 1000) | Date.now() |
PHP time() | Seconds | time() | round(microtime(true) * 1000) |
Java System.currentTimeMillis() | Milliseconds | System.currentTimeMillis() / 1000 | System.currentTimeMillis() |
Go time.Now().Unix() | Seconds | time.Now().Unix() | time.Now().UnixMilli() |
MySQL UNIX_TIMESTAMP() | Seconds | UNIX_TIMESTAMP() | UNIX_TIMESTAMP() * 1000 |
Discord requires seconds (10 digits) not milliseconds (13 digits). For Discord timestamps always use the seconds column. Use the Unix Timestamp Converter to verify your value before pasting into a Discord format code.
Common Mistakes With Unix Timestamps in Code
JavaScript milliseconds passed to Discord — Date.now() returns 13 digits. Passing this directly to a Discord timestamp format code produces a date in the year 275,760. Always divide by 1000.
PHP timezone not set — PHP time() is timezone-independent but date() and strtotime() use the system timezone if date_default_timezone_set() is not called explicitly. Always set timezone at the top of your file or in php.ini.
Python time.mktime() uses local timezone — Unlike datetime.timestamp() with timezone.utc, time.mktime() converts based on local timezone. On a server configured in GMT+5, the result is 5 hours different from UTC. Use timezone.utc explicitly.
Not handling milliseconds in JavaScript — When receiving timestamps from external APIs always check digit count. A 13-digit value is milliseconds and needs dividing by 1000 before use in any standard unix timestamp context.
Invalid date string in PHP strtotime() — strtotime() returns false on invalid input, not an error. Always use is_numeric() and check for false before using the result. Passing false silently to date() produces unexpected output.
32-bit overflow in 2038 — Any system storing timestamps as signed 32-bit integers will break on January 19 2038. PHP time() returns 32-bit on older systems. Use 64-bit storage everywhere.
Related Guides
- What Is a Unix Timestamp?
- Date to Unix Timestamp — Complete Guide
- Unix Timestamp to Date — Complete Guide
- Unix Timestamp Converter
Frequently Asked Questions
Ready to generate Discord timestamps?
Open the Generator