DateTime::getTimestamp
date_timestamp_get(DateTime $object): int
Converts between a DateTime object and Unix epoch seconds: getTimestamp reads the epoch (date_timestamp_get is its functional twin) and setTimestamp writes it back; contrasted with the seconds-vs-milliseconds API families of other languages.
C C23
#include <stdio.h>
#include <time.h>
int main(void) {
/* timegm is the UTC twin of mktime: struct tm -> epoch seconds */
struct tm tm = {.tm_year = 2024 - 1900, .tm_mon = 2, .tm_mday = 15, .tm_hour = 8, .tm_min = 30};
printf("%lld\n", (long long)timegm(&tm));
time_t ts = 1700000000;
printf("%lld\n", (long long)timegm(gmtime(&ts))); // gmtime is the inverse direction
return 0;
}
1710491400 1700000000
C++ C++26
#include <chrono>
#include <cstdio>
#include <ctime>
int main() {
std::tm tm = {}; // fields -> epoch via timegm (UTC twin of mktime)
tm.tm_year = 2024 - 1900;
tm.tm_mon = 2;
tm.tm_mday = 15;
tm.tm_hour = 8;
tm.tm_min = 30;
std::printf("%lld\n", (long long)timegm(&tm));
std::time_t ts = 1700000000; // system_clock round trip: epoch -> time_point -> epoch
auto tp = std::chrono::system_clock::from_time_t(ts);
std::printf("%lld\n", (long long)std::chrono::system_clock::to_time_t(tp));
return 0;
}
1710491400 1700000000
C# v11.0.100
var d = new DateTime(2024, 3, 15, 8, 30, 0, DateTimeKind.Utc);
Console.WriteLine(((DateTimeOffset)d).ToUnixTimeSeconds()); // seconds; ToUnixTimeMilliseconds is the millis school
var back = DateTimeOffset.FromUnixTimeSeconds(1700000000).UtcDateTime;
Console.WriteLine(back.ToString("yyyy-MM-dd HH:mm:ss"));
1710491400 2023-11-14 22:13:20
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2024, 3, 15, 8, 30, 0, 0, time.UTC)
fmt.Println(t.Unix()) // seconds, the POSIX convention
u := time.Unix(1700000000, 0).UTC()
fmt.Println(u.Format("2006-01-02 15:04:05"))
}
1710491400 2023-11-14 22:13:20
Java v27.0.1
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
Instant t = LocalDateTime.of(2024, 3, 15, 8, 30, 0).toInstant(ZoneOffset.UTC);
System.out.println(t.getEpochSecond()); // seconds; toEpochMilli() is the millis school
Instant back = Instant.ofEpochSecond(1700000000L);
System.out.println(back.atOffset(ZoneOffset.UTC).toLocalDateTime());
}
}
1710491400 2023-11-14T22:13:20
JavaScript v26.13.1
const d = new Date(Date.UTC(2024, 2, 15, 8, 30, 0)); // month is 0-based
console.log(d.getTime() / 1000); // Date stores MILLISECONDS; divide for POSIX seconds
const back = new Date(1700000000 * 1000);
console.log(back.toISOString().slice(0, 19).replace('T', ' '));
1710491400 2023-11-14 22:13:20
PHP v8.6.0
date_default_timezone_set('UTC');
$d = new DateTime('2024-03-15 08:30:00', new DateTimeZone('UTC'));
echo $d->getTimestamp(), PHP_EOL; // object -> epoch seconds
echo date_timestamp_get($d), PHP_EOL; // functional twin, same value
$d->setTimestamp(1700000000); // the reverse direction
echo $d->format('Y-m-d H:i:s'), PHP_EOL;
1710491400 1710491400 2023-11-14 22:13:20
Python v3.15.2
from datetime import datetime, timezone
d = datetime(2024, 3, 15, 8, 30, 0, tzinfo=timezone.utc)
print(int(d.timestamp())) # POSIX seconds; a naive datetime would use the local tz
back = datetime.fromtimestamp(1700000000, tz=timezone.utc)
print(back.strftime('%Y-%m-%d %H:%M:%S'))
1710491400 2023-11-14 22:13:20
Rust v1.99.1
use chrono::{DateTime, TimeZone, Utc};
fn main() {
let d: DateTime<Utc> = Utc.with_ymd_and_hms(2024, 3, 15, 8, 30, 0).unwrap();
println!("{}", d.timestamp()); // i64 POSIX seconds; timestamp_millis() is the millis school
let back = Utc.timestamp_opt(1700000000, 0).single().unwrap();
println!("{}", back.format("%Y-%m-%d %H:%M:%S"));
}
1710491400 2023-11-14 22:13:20
TypeScript v26.13.1
const d: Date = new Date(Date.UTC(2024, 2, 15, 8, 30, 0)); // month is 0-based
console.log(d.getTime() / 1000); // Date stores MILLISECONDS; divide for POSIX seconds
const back: Date = new Date(1700000000 * 1000);
console.log(back.toISOString().slice(0, 19).replace('T', ' '));
1710491400 2023-11-14 22:13:20
Language differences
C: the epoch round trip in C is the pair timegm (struct tm -> epoch, UTC) and gmtime (epoch -> struct tm, UTC) (example); mktime/localtime are their local-timezone twins; timegm is a GNU/BSD extension rather than ISO C — this repo pins -D_GNU_SOURCE, and the code must include <time.h> explicitly.
C++: line one uses the same timegm path as C; since C++11, std::chrono::system_clock::to_time_t/from_time_t converts between time_point and epoch seconds (example line two, a round trip); the millis school is expressible via duration_cast<milliseconds>(tp.time_since_epoch()).
C#: DateTimeOffset.ToUnixTimeSeconds() is the seconds-school entry (example), with ToUnixTimeMilliseconds() as its millis-school twin; a bare DateTime must be converted to DateTimeOffset (cast in the example) or carry DateTimeKind.Utc, otherwise the local zone leaks in.
Go: time.Time.Unix() reads seconds (example), UnixMilli()/UnixNano() are the millis/nanos schools; the reverse is time.Unix(sec, nsec) followed by .UTC() before formatting (example).
Java: Instant.getEpochSecond() is the seconds school (example), toEpochMilli() the millis school; Instant is always UTC with no timezone ambiguity — the least error-prone entry point on the Java side; LocalDateTime has no epoch concept, so conversions need an explicit ZoneOffset (example line one).
JavaScript: Date stores milliseconds natively (the epoch-millis school) — POSIX seconds require getTime()/1000 (example line one); the reverse is new Date(sec * 1000) (example line two); the 0-based month trap is covered in the date_create entry.
PHP: getTimestamp() returns int seconds (example line one; date_timestamp_get is its twin and prints the same value); the reverse is setTimestamp() (example line three); date entries always pin the zone first with date_default_timezone_set('UTC') so epoch assertions stay stable across environments.
Python: datetime.timestamp() returns float seconds (example casts to int); a naive datetime is interpreted in the local timezone — cross-platform determinism requires tzinfo=timezone.utc; the reverse is fromtimestamp(epoch, tz=timezone.utc) (example line two).
Rust: chrono's DateTime<Utc>.timestamp() returns i64 seconds (example line one), timestamp_millis() the millis school; the reverse, Utc.timestamp_opt(sec, nsec), returns an Option that forces handling of out-of-range values (example line two).
TypeScript: same millis school as JavaScript; Temporal.Instant will replace Date with nanosecond precision and an explicit epoch origin once it lands — in the meantime, date-fns getTime/secondsToMilliseconds wrappers cover the conversion.