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

如何在CanvasJS图表中利用PHP DateTime显示带毫秒的格式化时间?

Fixing Millisecond Display in CanvasJS Data Point Labels

Got it, let's break down why your milliseconds aren't showing up and fix it using the DateTime logic you already have working elsewhere.

The root issue is that strtotime() doesn't preserve milliseconds when parsing your setTime value—it truncates that detail entirely, which is why date() can't output it. Your existing DateTime code already handles milliseconds correctly, so we just need to reuse that approach for your CanvasJS data points.

Here's the adjusted code for generating your data points:

// Use DateTime to parse and format the time (just like your working display code)
$dateTime = new DateTime($row['setTime']);
$formattedLabel = $dateTime->format("g:i:s:v a F j, Y");

// Build and push the data point
$a = array(
    "y" => $row['setPercent'],
    'label' => $formattedLabel
);
array_push($dataPoints, $a);

Why this works:

  • DateTime natively supports parsing and formatting milliseconds (the v format character handles this), unlike strtotime() which only works with second-level precision.
  • You're using the exact same formatting pattern that already works for your other date display, so you'll get consistent, correct output including milliseconds.

If you prefer more concise code, you can combine it into a single line:

array_push($dataPoints, array(
    "y" => $row['setPercent'],
    'label' => (new DateTime($row['setTime']))->format("g:i:s:v a F j, Y")
));

This should make your CanvasJS chart labels display the full date/time with milliseconds, matching the behavior of your existing date display code.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:17:36