IntlDateFormatter
IntlDateFormatter::create(?string $locale, int $dateType = IntlDateFormatter::FULL, int $timeType = IntlDateFormatter::FULL, IntlTimeZone|DateTimeZone|string|null $timezone = null, IntlCalendar|int|null $calendar = null, ?string $pattern = null): ?IntlDateFormatter
ICU 日期格式化绑定:locale + 预定义样式(SHORT/LONG/FULL)或显式模式串决定输出形态,可解析回时间戳。与 JS Intl.DateTimeFormat、Java DateTimeFormatter.ofLocalizedDate 同族。本条目示例固定 UTC 时间戳并用显式模式,规避 ICU 版本漂移。
C C23
#include <stdio.h>
#include <time.h>
int main(void) {
/* C 标准库没有现成的 locale 样式:strftime 只接受手写模式串 */
time_t ts = (time_t)1795262400; /* 2026-11-21 12:00 UTC */
struct tm utc = *gmtime(&ts);
char buf[40];
strftime(buf, sizeof buf, "%m/%d/%Y", &utc);
printf("%s\n", buf);
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M", &utc);
printf("%s\n", buf);
return 0;
}
11/21/2026 2026-11-21 12:00
C++ C++26
#include <cstdio>
#include <ctime>
int main() {
// 与 C 相同:strftime 手写模式;iostream 要 imbue locale 才有样式可言
std::time_t ts = 1795262400; // 2026-11-21 12:00 UTC
std::tm utc = *std::gmtime(&ts);
char buf[40];
std::strftime(buf, sizeof buf, "%m/%d/%Y", &utc);
std::printf("%s\n", buf);
std::strftime(buf, sizeof buf, "%Y-%m-%d %H:%M", &utc);
std::printf("%s\n", buf);
return 0;
}
11/21/2026 2026-11-21 12:00
C# v11.0.100
// CultureInfo 决定标准日期模式;"d"/"D" 分别是短/长模式
using System.Globalization;
var ts = DateTimeOffset.FromUnixTimeSeconds(1795262400L).UtcDateTime; // 2026-11-21 12:00 UTC
Console.WriteLine(ts.ToString("d", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(ts.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture));
11/21/2026 2026-11-21 12:00
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
ts := time.Unix(1795262400, 0).UTC() // 2026-11-21 12:00 UTC
// 布局串是显式模式;标准库没有 locale 感知的样式
fmt.Println(ts.Format("01/02/2006"))
fmt.Println(ts.Format("2006-01-02 15:04"))
// 本地化样式在 golang.org/x/text(见 notes)
}
11/21/2026 2026-11-21 12:00
Java v27.0.1
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Instant ts = Instant.ofEpochSecond(1795262400L); // 2026-11-21 12:00 UTC
// JDK 自带 locale 数据(不是 ICU):ofLocalizedDate 选取 locale 对应模式
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(Locale.US).withZone(ZoneOffset.UTC).format(ts));
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.US).withZone(ZoneOffset.UTC).format(ts));
// 显式模式与 ICU 版本无关,各语言同理
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
.withZone(ZoneOffset.UTC).format(ts));
}
}
11/21/26 Nov 21, 2026 2026-11-21 12:00
JavaScript v26.13.1
const ts = 1795262400; // 2026-11-21 12:00 UTC
// Intl.DateTimeFormat 与 PHP 同属 ICU 家族,locale 按调用传入
const opts = { timeZone: 'UTC', year: 'numeric', month: '2-digit', day: '2-digit' };
console.log(new Intl.DateTimeFormat('en-US', opts).format(new Date(ts * 1000)));
console.log(new Intl.DateTimeFormat('en-GB', opts).format(new Date(ts * 1000)));
11/21/2026 21/11/2026
PHP v8.6.0
// IntlDateFormatter 是 PHP 对 ICU 的绑定:locale + 样式/模式决定输出形态
$fmt = new IntlDateFormatter('en_US', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'UTC');
$fmt->setPattern('MM/dd/yyyy'); // 显式模式:任何 ICU 版本下结果一致
echo $fmt->format(1795262400), "\n"; // 2026-11-21 12:00 UTC
$fmt->setPattern('yyyy-MM-dd HH:mm');
echo $fmt->format(1795262400), "\n";
11/21/2026 2026-11-21 12:00
Python v3.15.2
from datetime import datetime, timezone
ts = datetime.fromtimestamp(1795262400, tz=timezone.utc) # 2026-11-21 12:00 UTC
# 标准库 strftime 只支持手写模式(locale 感知的 %x 需要 setlocale + OS locale)
print(ts.strftime('%m/%d/%Y'))
print(ts.strftime('%Y-%m-%d %H:%M'))
# 真正的 locale 样式来自第三方库 Babel(见 notes)
11/21/2026 2026-11-21 12:00
Rust v1.99.1
use chrono::{TimeZone, Utc};
fn main() {
// chrono 是事实标准日期 crate(cargo);格式化用显式模式
let ts = Utc.timestamp_opt(1795262400, 0).unwrap(); // 2026-11-21 12:00 UTC
println!("{}", ts.format("%m/%d/%Y"));
println!("{}", ts.format("%Y-%m-%d %H:%M"));
}
11/21/2026 2026-11-21 12:00
TypeScript v26.13.1
const ts: number = 1795262400; // 2026-11-21 12:00 UTC
// Intl.DateTimeFormat 与 PHP 同属 ICU 家族,locale 按调用传入
const opts: Intl.DateTimeFormatOptions = { timeZone: 'UTC', year: 'numeric', month: '2-digit', day: '2-digit' };
console.log(new Intl.DateTimeFormat('en-US', opts).format(new Date(ts * 1000)));
console.log(new Intl.DateTimeFormat('en-GB', opts).format(new Date(ts * 1000)));
11/21/2026 21/11/2026
语言差异说明
PHP:IntlDateFormatter 是 ICU 的日期格式化绑定(intl 扩展),构造参数 = locale + dateType/timeType(NONE/SHORT/MEDIUM/LONG/FULL + 8.0 起 RELATIVE_*)+ 时区 + 日历 + 可选模式串;format() 吃时间戳/DateTime/数组,配套 parse() 可逆。关键决策:示例刻意用 setPattern('MM/dd/yyyy') 显式模式而非 SHORT 样式——短样式映射到 locale 模式,会随 CLDR/ICU 版本漂移(本机 ICU 79 下 en_US SHORT 已从 11/21/2026 变成两位年 11/21/26,JS/Java 同现象),而显式模式逐字符确定,任何 ICU 版本输出一致。ICU 版本敏感性分析:日期里只有带序数词/周期名的长文本(如 Nov 21, 2026、星期几)与短年样式敏感;纯数字模式(MM/dd/yyyy、yyyy-MM-dd HH:mm)在 ICU 各版本间逐字节稳定。对照:本条目 10 语言输出里 Java SHORT 给出 11/21/26(JDK 自带数据,与 ICU 无关地也走了两位年)。
C/C++:标准库没有 locale 样式的日期格式化,strftime 只认手写模式;本地化文本要 setlocale(LC_TIME) + 系统安装对应 locale,可移植性差,故示例走显式模式。
C#:ToString("d", culture) 用 culture 的短模式(.NET 内置数据,en-US 是四位年 11/21/2026);InvariantCulture + 显式模式最稳。
Java:DateTimeFormatter.ofLocalizedDate(style) 是 JDK 自带 locale 数据(非 ICU、非 OS);withLocale().withZone() 链式参数化,与 ICU 流派对照阅读。
JS/TS:Intl.DateTimeFormat(locale, opts) 是同族原生 API(V8/浏览器内置 ICU);示例用显式 numeric 字段而非 dateStyle:'short',原因同 PHP——短样式随 ICU 版本变(Node 26 的 en-US 短样式已是 11/21/26)。
Python:标准库只有 strftime(locale 感知的 %x/%c 需 setlocale + OS locale,不可移植);ICU 级样式在第三方 Babel(format_date(ts, locale='en_US'))。
Go:time.Format 布局串纯显式;本地化样式在 golang.org/x/text(message.NewPrinter(locale))。
Rust:chrono crate(校验器 cargo 模式)format("%m/%d/%Y") 与 strftime 同族;ICU 级在 icu/icu_datetime crate。
选型小结:要跨 ICU 版本稳定就显式模式 + 固定时区;要「最地道本地样式」就接受 ICU/CLDR 升级带来的输出微调(样式表本身可查 ICU User Guide 的 Date/Time Format Syntax)。