惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

沈唁志

使用 Trusted Publishing 提升 npm 包发布安全性 Linux 服务器实现 Word 转图片方案 2025|在代码之外,我学会了旅行、牵手和拥抱 使用 GitHub Actions 自动同步 Docker 镜像到 CNB 怎么申请开具中国税收居民身份证明? HMAC 签名编码的坑:Go 和 PHP 的不同处理方式 Bitwarden Secrets Manager:简化 DevOps 的机密管理 MySQL 字符集与大小写敏感性解析 在命令行中输出带颜色的日志
PHP 中生成带毫秒的时间戳
沈唁 · 2025-01-02 · via 沈唁志

今天在对接一个 API 的时候,发现需要生成高精度的时间戳,格式为yyyyMMddHH24mmssSSS

本文将介绍两种常见的实现方式,并讨论它们的优缺点。

时间格式解析

格式 yyyyMMddHH24mmssSSS 的含义如下:

  • yyyy:四位数的年份(例如:2025)。
  • MM:两位数的月份(01-12)。
  • dd:两位数的日期(01-31)。
  • HH24:两位数的小时(24 小时制,00-23)。
  • mm:两位数的分钟(00-59)。
  • ss:两位数的秒(00-59)。
  • SSS:三位数的毫秒(000-999)。

例如,时间 2025-01-02 11:30:45.123 的格式化结果为:20250102113045123

使用 DateTime 类实现

以下是使用 DateTime 类生成毫秒时间戳的代码示例:

<?php
$dateTime = new DateTime();
// 获取当前时间的微秒数并计算为毫秒
$milliseconds = intval($dateTime->format('u') / 1000);
// 格式化时间
$formattedTime = $dateTime->format("YmdHi") . $dateTime->format("s") . sprintf("%03d", $milliseconds);
echo $formattedTime;

代码解析

  1. $dateTime->format('u') 返回当前时间的微秒(6 位数,例如 123456)。
  2. intval($dateTime->format('u') / 1000) 将微秒转换为毫秒(3 位数,例如 123)。
  3. 使用 sprintf("%03d", $milliseconds) 确保毫秒部分始终为 3 位数(不足时补零)。

示例输出

假设当前时间为 2025-01-02 11:30:45.123456,输出结果为:

20250102113045123

使用 microtime 函数实现

另一种方法是结合 microtime()date() 函数:

<?php
$microtime = microtime(true);
// 格式化时间到秒
$formattedDate = date('YmdHis', floor($microtime));
// 获取毫秒部分
$milliseconds = sprintf('%03d', ($microtime - floor($microtime)) * 1000);
// 拼接毫秒
$formattedDate .= $milliseconds;
echo $formattedDate;

代码解析

  1. microtime(true) 返回当前 Unix 时间戳,包含秒和小数部分。
  2. floor($microtime) 获取整数秒部分。
  3. ($microtime - floor($microtime)) * 1000 提取小数部分并转换为毫秒。
  4. 最终拼接秒部分和毫秒部分,生成完整的时间戳。

示例输出

假设当前时间为 2025-01-02 15:30:45.123456,输出结果为:

20250102153045123

对比分析

特性 使用 DateTime 使用 microtime()
代码简洁性 更加现代化,语义清晰 较为传统,需要手动处理毫秒
精度 取决于系统支持的时间精度 依赖 microtime() 的实现
扩展性 更容易与其他 DateTime 操作结合 适合处理与 Unix 时间戳相关的逻辑

统一验证输出

为了验证两种方法的输出是否一致,可以添加以下代码:

if ($formattedTime === $formattedDate) {
    echo "两种方法的输出一致:$formattedTime\n";
} else {
    echo "两种方法的输出不一致:\n第一种方法:$formattedTime\n第二种方法:$formattedDate\n";
}

总结

  • 如果你的项目主要使用 DateTime 类,建议采用第一种方法,代码语义更加清晰。
  • 如果需要与 microtime() 或 Unix 时间戳直接交互,可以选择第二种方法。

选择哪种方式主要取决于项目需求和代码风格偏好。希望本文对你在生成带毫秒的时间戳方面有所帮助!