Discord Guide

Unix Timestamp in Python, JavaScript, and PHP

June 29, 2026 · 11 min read

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 integerseconds 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"
Pro Tip:In PHP always use DateTimeImmutable instead of DateTime when working with timestamps. DateTimeImmutable never modifies the original object — every modification returns a new instance. This prevents a whole class of bugs where you accidentally mutate a shared date object in a loop or chained operation.

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:

LanguageDefaultGet SecondsGet Milliseconds
Python time.time()Float secondsint(time.time())int(time.time() * 1000)
JavaScript Date.now()MillisecondsMath.floor(Date.now() / 1000)Date.now()
PHP time()Secondstime()round(microtime(true) * 1000)
Java System.currentTimeMillis()MillisecondsSystem.currentTimeMillis() / 1000System.currentTimeMillis()
Go time.Now().Unix()Secondstime.Now().Unix()time.Now().UnixMilli()
MySQL UNIX_TIMESTAMP()SecondsUNIX_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.

Pro Tip:When working with timestamps across multiple languages in the same system, pick one canonical format — UTC seconds as a 64-bit integer — and convert at the boundaries. Store UTC seconds in your database, pass UTC seconds between services, and convert to milliseconds or local time only at the display layer. This eliminates an entire class of cross-language timestamp bugs.

Common Mistakes With Unix Timestamps in Code

JavaScript milliseconds passed to DiscordDate.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


Frequently Asked Questions

Use int(datetime.now(timezone.utc).timestamp()) from the datetime module. Import datetime and timezone from datetime. This returns the current unix timestamp as an integer in seconds with UTC explicitly set to avoid local timezone issues. For a float with microseconds use time.time() from the time module.
Use Math.floor(Date.now() / 1000) for seconds. Date.now() returns milliseconds by default — always divide by 1000. For a specific date use Math.floor(new Date('2026-06-29T12:00:00Z').getTime() / 1000). Always use ISO 8601 format strings ending in Z for consistent UTC parsing across browsers.
Use time() — it returns the current unix timestamp as an integer in seconds since January 1 1970 at 00:00:00 GMT. For microseconds use microtime(true). For the timestamp at request start use $_SERVER['REQUEST_TIME']. Always call date_default_timezone_set('UTC') at the top of your file.
Date.now() returns the number of milliseconds elapsed since the epoch — midnight at beginning of January 1 1970 UTC. It returns a 13-digit number. Divide by 1000 and use Math.floor() to get the standard 10-digit seconds value. Note that in browsers with privacy.resistFingerprinting enabled, reduced time precision may round the result to 2ms.
time() is a PHP built-in function that returns the current unix timestamp — the current time measured in seconds since the Unix Epoch January 1 1970 00:00:00 GMT. It takes no parameters and returns an integer. Available since PHP 4+. Note that unix timestamps do not contain local timezone information — use DateTimeImmutable for timezone-aware date handling.
Use new Date(timestamp * 1000) — multiply by 1000 because the Date object expects milliseconds not seconds. Then use .toUTCString() for UTC output, .toISOString() for ISO 8601 format, or .toLocaleDateString() for locale-formatted output. Use getFullYear(), getMonth(), getDate() etc to access individual components.
JavaScript was designed for browser environments where sub-second precision matters for animations, timers, and UI interactions. Using milliseconds as the default gives 1000x more granularity than seconds. Standard unix timestamps use seconds because most operating systems and server-side systems do not need millisecond precision. This mismatch is the most common source of unix timestamp bugs when working across JavaScript and server-side programming languages.
Check the digit count — 10 digits means seconds, 13 digits means milliseconds: ts.length === 13 ? parseInt(ts) : parseInt(ts) * 1000. This pattern handles both formats before passing to new Date(). For Discord specifically, always output 10-digit seconds using Math.floor(Date.now() / 1000).

Ready to generate Discord timestamps?

Open the Generator

Did You Miss These?

Discord Guide

Generate a Discord Timestamp

Free tool — all 7 formats, live Discord preview, auto timezone detection.

Open Generator →
Discord Guide8 min read

Jun 29, 2026

Date to Unix Timestamp — Complete Guide

How to convert any date to a Unix timestamp in JavaScript, Python, PHP, Java, MySQL and more. Code examples and common mistakes covered.

Read Article →
Discord Guide8 min read

Jun 29, 2026

Unix Timestamp to Date — Complete Guide

How to convert a Unix timestamp to a human readable date in JavaScript, Python, PHP, Java, MySQL and more. Code examples and common mistakes covered.

Read Article →
Discord Guide8 min read

Jun 29, 2026

Discord Countdown Timer — Complete Guide

How to create a live Discord countdown timer using timestamps and bots. Step-by-step guide with format codes and common mistakes.

Read Article →
Discord Guide8 min read

Jun 28, 2026

What Is a Unix Timestamp?

Learn what a Unix timestamp is, how it works, why it starts in 1970, and how it connects to Discord timestamps.

Read Article →
Discord Guide6 min read

Jun 27, 2026

How to Make Time Zone Messages in Discord

How to send Discord messages that automatically show the right local time for every reader worldwide.

Read Article →
Discord Guide7 min read

Jun 26, 2026

How to Set a Time on a Discord Message

Learn how to use Discord timestamps to set a time on any message that auto-converts for every time zone.

Read Article →
Discord Guide8 min read

Jun 8, 2026

What Is a Discord Timestamp?

Learn what a Discord timestamp is, how discord time codes work, all 7 formats explained with examples.

Read Article →