返回列表
各编程语言时间戳操作指南(PHP/JavaScript/Python/Java)
PHP
// 获取当前时间戳(秒)
$timestamp = time();
// 时间戳转日期
$date = date('Y-m-d H:i:s', $timestamp);
// 日期转时间戳
$timestamp = strtotime('2026-05-23 15:30:00');
// 毫秒级时间戳
$milliseconds = round(microtime(true) * 1000);
JavaScript
// 获取当前时间戳(毫秒)
const timestampMs = Date.now();
// 获取秒级时间戳
const timestampSec = Math.floor(Date.now() / 1000);
// 时间戳转日期
const date = new Date(1748016000000);
const formatted = date.toLocaleString('zh-CN');
// 日期转时间戳
const timestamp = new Date('2026-05-23 15:30:00').getTime();
Python
import time
from datetime import datetime
# 获取当前时间戳
timestamp = int(time.time())
# 时间戳转日期
dt = datetime.fromtimestamp(1748016000)
# 日期转时间戳
timestamp = int(datetime.strptime('2026-05-23 15:30:00', '%Y-%m-%d %H:%M:%S').timestamp())
Java
// 获取当前时间戳
long timestamp = System.currentTimeMillis(); // 毫秒
// 时间戳转日期
Date date = new Date(1748016000000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
// 日期转时间戳
String dateStr = "2026-05-23 15:30:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long timestamp = sdf.parse(dateStr).getTime();