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

PHP中date()函数嵌入<i>标签失效,单行实现带样式日期疑问

Why Your Single-Line date() Call Fails with <i> Tags

Hey there! Nice work figuring out a solution by splitting your code—let's dig into why that original single-line approach didn't work, and clear up what's going on with PHP's date() function.

The Root of the Problem

PHP's date() function treats certain characters in its format string as special format specifiers—like Y for 4-digit year, M for 3-letter month, and crucially, i for two-digit minutes (e.g., 05 for 5 past the hour).

When you tried echo date("<b>Y</b>.<i>M</i>.<i>d</i>"), here's what happened:

  • <b> and </b> worked fine because b isn't a recognized format specifier—PHP just passed those tags through as plain text.
  • But the i inside <i> got picked up as the minutes specifier! So PHP replaced that i with the current minute value (like 03 or 17), turning your <i> into something like <03>—hence the weird < minute >Jan output you saw.

Fixes to Try

If you still want a single-line solution, or just want to know your options:

  1. Escape the format specifier
    You can tell PHP to treat the i as a regular character by adding a backslash before it. Since we're using double quotes, we need to escape the backslash too (or wrap the format string in single quotes to avoid that extra step):

    // Using double quotes (needs escaped backslash)
    echo date("<b>Y</b>.<\\i>M<\\/i>.<\\i>d<\\/i>");
    
    // Using single quotes (cleaner)
    echo date('<b>Y</b>.<\i>M<\/i>.<\i>d<\/i>');
    

    This works, but it's a bit messy to read.

  2. Stick to your split approach (cleanest!)
    Separating the date() calls from your HTML tags avoids any specifier conflicts entirely, and makes your code easier to read at a glance:

    echo "Today's date is: <b>" . date("Y") . "</b>.<i>" . date("M") . "." . date("d") . "</i>";
    
  3. Use the DateTime class (modern PHP practice)
    For more flexibility (and to avoid these kinds of gotchas long-term), consider using PHP's DateTime class instead of the older date() function:

    $today = new DateTime();
    echo "Today's date is: <b>{$today->format('Y')}</b>.<i>{$today->format('M')}.{$today->format('d')}</i>";
    

Hope that clears up why your initial attempt hit a snag—great job troubleshooting on your own!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:33:22