理解 Unix 时间戳:开发者完整指南

Development2026-05-20TryQuickToolBox

什么是 Unix 时间戳?

Unix 时间戳是一种以累计秒数跟踪时间的方式。它计算自1970 年 1 月 1 日 00:00:00 UTC(称为"Unix 纪元"的参考点)以来经过的秒数。

例如,Unix 时间戳 1719000000 代表一个特定时刻:2024 年 6 月 22 日约 00:00 UTC。每秒,这个数字增加一。

为什么要使用 Unix 时间戳?

Unix 时间戳在软件开发中无处不在,原因如下:

秒 vs. 毫秒

Unix 时间戳有两种常见格式:

一个常见的 bug 来源是混淆这两种格式。如果你将毫秒时间戳传递给期望秒的函数,日期将完全错误(大约在未来 50,000 年)。

在不同语言中使用时间戳

JavaScript

const now = Date.now(); // 毫秒
const seconds = Math.floor(Date.now() / 1000);
const date = new Date(seconds * 1000);

Python

import time
seconds = int(time.time())
from datetime import datetime
dt = datetime.fromtimestamp(seconds)

PHP

$timestamp = time(); // 秒
$date = date('Y-m-d H:i:s', $timestamp);

MySQL

SELECT UNIX_TIMESTAMP(); -- 当前时间戳
SELECT FROM_UNIXTIME(1719000000); -- 转换为日期

常见陷阱

TryQuickToolBox 时间戳转换器

我们的免费工具在 Unix 时间戳(秒和毫秒)、ISO 8601 日期、UTC 时间和本地时间之间互相转换。自动检测意味着你可以粘贴任何格式并获得即时结果。点击任意结果即可复制到剪贴板。

立即转换时间戳 →