You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

毫秒级时间戳转换问题:无法获取末尾三位毫秒数

Solution for Getting Millisecond-Level Timestamps 30 Days Ago in PHP

Got it, let's break this down for you. The issue here is that the functions you're using (time() and strtotime()) only work with second-level timestamps—they don't track milliseconds at all. That's why you're missing those last three digits from your result. Here are a couple of ways to fix this, depending on what you need:

Option 1: Reuse the Milliseconds from the API Response

If you want to keep the same millisecond value that the API returned (974 in your example) and just calculate the 30-day-ago timestamp for that exact millisecond, you can split the API timestamp into seconds and milliseconds, then adjust the seconds part:

// Your API's millisecond timestamp
$apiTimestamp = 1520683685974;

// Extract the millisecond portion (last 3 digits)
$milliseconds = $apiTimestamp % 1000;

// Calculate 30 days ago in seconds (using your original logic)
$currentSec = time();
$thirtyDaysAgoSec = strtotime('-30 days', $currentSec);

// Combine the adjusted seconds with the original milliseconds
$thirtyDaysAgoMs = $thirtyDaysAgoSec * 1000 + $milliseconds;

echo $thirtyDaysAgoMs; // Outputs something like 1520682524974

Option 2: Get the Exact Current Milliseconds 30 Days Ago

If you need the actual millisecond timestamp for the same time of day 30 days ago (not just reusing the API's millisecond value), use microtime() which tracks microseconds (we can convert those to milliseconds):

// Get current time as a float (seconds + microseconds)
$currentMsFloat = microtime(true);

// Calculate 30 days in seconds (30 * 24 * 60 * 60 = 2592000)
$thirtyDaysInSec = 2592000;

// Subtract 30 days and convert to integer milliseconds
$thirtyDaysAgoMs = (int)(($currentMsFloat - $thirtyDaysInSec) * 1000);

echo $thirtyDaysAgoMs;

Option 3: Use DateTime for More Control

If you prefer using PHP's DateTime class (which is more readable for date manipulations), you can access microseconds and convert them to milliseconds:

// Create a DateTime object for now
$now = new DateTime();

// Subtract 30 days using DateInterval
$now->sub(new DateInterval('P30D'));

// Get the second-level timestamp
$timestampSec = $now->getTimestamp();

// Get microseconds, then convert to milliseconds (take first 3 digits)
$microseconds = $now->format('u');
$milliseconds = (int)($microseconds / 1000);

// Combine into a millisecond-level timestamp
$thirtyDaysAgoMs = $timestampSec * 1000 + $milliseconds;

echo $thirtyDaysAgoMs;

Key Notes:

  • time() and strtotime() will never return milliseconds—they're designed for second-level precision only.
  • If the API returns dynamic millisecond values (not always 974), use Option 2 or 3 to capture the actual current millisecond offset.

内容的提问来源于stack exchange,提问作者david

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:19:10