How to Convert a Date to a Unix Timestamp
Updated: May 2026
Building a Unix timestamp from a date string or a form input is trickier than it looks. The most common bug is treating the input as if it were in UTC when it is actually local time — or the reverse. This guide shows the correct approach in every major language.
Free · No upload · Instant
The timezone trap
When you write "2025-01-01 00:00:00", does that string represent midnight UTC or midnight in your local timezone? The answer changes your Unix timestamp by the timezone's offset in seconds — up to ±14 hours (±50 400 seconds).
The rule: always be explicit about the timezone when building a timestamp from a string. Never rely on implicit defaults in production code.
ISO 8601 strings ending in Z (e.g. 2025-01-01T00:00:00Z) are unambiguously UTC. Strings without a timezone suffix are local time in most runtimes — a dangerous default.
JavaScript / TypeScript
JavaScript// From a UTC ISO string — always safe
const ts = Math.floor(new Date('2025-01-01T00:00:00Z').getTime() / 1000);
console.log(ts); // 1735689600
// Current timestamp in seconds
const now = Math.floor(Date.now() / 1000);
// From a local date (browser timezone) — user input from <input type="date">
const dateStr = '2025-01-01';
const timeStr = '12:00:00';
const localDate = new Date(`${dateStr}T${timeStr}`);
const tsLocal = Math.floor(localDate.getTime() / 1000);
// From a specific IANA timezone (using Intl trick)
function dateToTimestampInTz(dateStr, timeStr, tz) {
const naive = new Date(`${dateStr}T${timeStr}`);
const utcMs = naive.getTime() - getOffsetMs(naive, tz);
return Math.floor(utcMs / 1000);
}
function getOffsetMs(date, tz) {
const a = date.toLocaleString('en-US', { timeZone: 'UTC' });
const b = date.toLocaleString('en-US', { timeZone: tz });
return new Date(b) - new Date(a);
}
console.log(dateToTimestampInTz('2025-01-01', '00:00:00', 'Europe/Paris'));
// → 1735685400 (midnight Paris = 23:00 UTC on Dec 31)
Python
Python 3from datetime import datetime, timezone
from zoneinfo import ZoneInfo # Python 3.9+
# From a UTC datetime
dt_utc = datetime(2025, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
print(int(dt_utc.timestamp())) # 1735689600
# From a string with explicit UTC
dt = datetime.fromisoformat('2025-01-01T00:00:00+00:00')
print(int(dt.timestamp())) # 1735689600
# From local time in a specific timezone
tz = ZoneInfo('America/New_York')
dt_ny = datetime(2024, 12, 31, 19, 0, 0, tzinfo=tz)
print(int(dt_ny.timestamp())) # 1735689600 — same instant as midnight UTC
# Current timestamp
import time
print(int(time.time())) # seconds
PHP
PHP 8// From a UTC string
echo strtotime('2025-01-01 00:00:00 UTC'); // 1735689600
// Using DateTimeImmutable with explicit timezone
$dt = new DateTimeImmutable('2025-01-01 00:00:00', new DateTimeZone('UTC'));
echo $dt->getTimestamp(); // 1735689600
// In a specific timezone
$dt = new DateTimeImmutable('2025-01-01 01:00:00', new DateTimeZone('Europe/Paris'));
echo $dt->getTimestamp(); // 1735689600 — Paris UTC+1
// Current timestamp
echo time(); // seconds since epoch
SQL
PostgreSQL-- Date string to Unix timestamp
SELECT EXTRACT(EPOCH FROM '2025-01-01 00:00:00+00'::timestamptz)::bigint;
-- → 1735689600
-- With timezone
SELECT EXTRACT(EPOCH FROM '2025-01-01 01:00:00 Europe/Paris'::timestamptz)::bigint;
-- → 1735689600
MySQL
-- Assumes UTC session or set explicitly
SET time_zone = '+00:00';
SELECT UNIX_TIMESTAMP('2025-01-01 00:00:00');
-- → 1735689600
No-code: use the Flowfiles converter
If you just need the timestamp for a specific date without writing code, the Flowfiles Unix timestamp converter lets you pick a date and time, choose your timezone, and copies the resulting timestamp instantly. It handles the UTC offset calculation automatically, which avoids the most common timezone bug.