<?php
declare(strict_types=1);
$cases = [];
// Case 1: DateTimeZone::listIdentifiers total & region counts
$all = DateTimeZone::listIdentifiers();
$regions = [
'africa' => count(DateTimeZone::listIdentifiers(DateTimeZone::AFRICA)),
'america' => count(DateTimeZone::listIdentifiers(DateTimeZone::AMERICA)),
'antarctica' => count(DateTimeZone::listIdentifiers(DateTimeZone::ANTARCTICA)),
'arctic' => count(DateTimeZone::listIdentifiers(DateTimeZone::ARCTIC)),
'asia' => count(DateTimeZone::listIdentifiers(DateTimeZone::ASIA)),
'atlantic' => count(DateTimeZone::listIdentifiers(DateTimeZone::ATLANTIC)),
'australia' => count(DateTimeZone::listIdentifiers(DateTimeZone::AUSTRALIA)),
'europe' => count(DateTimeZone::listIdentifiers(DateTimeZone::EUROPE)),
'indian' => count(DateTimeZone::listIdentifiers(DateTimeZone::INDIAN)),
'pacific' => count(DateTimeZone::listIdentifiers(DateTimeZone::PACIFIC)),
'utc' => count(DateTimeZone::listIdentifiers(DateTimeZone::UTC)),
];
$countActual = [
'total' => count($all),
'sum_regions' => array_sum($regions),
'regions' => $regions,
];
$countExpected = [
'total' => 419,
'sum_regions' => 419,
'regions' => [
'africa' => 52,
'america' => 144,
'antarctica' => 11,
'arctic' => 1,
'asia' => 82,
'atlantic' => 10,
'australia' => 11,
'europe' => 58,
'indian' => 11,
'pacific' => 38,
'utc' => 1,
],
];
$cases[] = [
'name' => 'timezone_identifier_counts',
'passed' => $countActual === $countExpected,
'expected' => $countExpected,
'actual' => $countActual,
];
// Case 2: Fixed UTC instant converted across Africa/America/Asia/Europe/Pacific/UTC (winter & summer)
$winterUtc = new DateTimeImmutable('2026-01-15T12:00:00+00:00');
$summerUtc = new DateTimeImmutable('2026-07-15T12:00:00+00:00');
$zones = ['UTC', 'Asia/Shanghai', 'America/New_York', 'Europe/London', 'Africa/Cairo', 'Pacific/Auckland'];
$convActual = [];
foreach ($zones as $z) {
$tz = new DateTimeZone($z);
$convActual[$z] = [
'jan' => $winterUtc->setTimezone($tz)->format('Y-m-d H:i P'),
'jul' => $summerUtc->setTimezone($tz)->format('Y-m-d H:i P'),
];
}
$convExpected = [
'UTC' => ['jan' => '2026-01-15 12:00 +00:00', 'jul' => '2026-07-15 12:00 +00:00'],
'Asia/Shanghai' => ['jan' => '2026-01-15 20:00 +08:00', 'jul' => '2026-07-15 20:00 +08:00'],
'America/New_York' => ['jan' => '2026-01-15 07:00 -05:00', 'jul' => '2026-07-15 08:00 -04:00'],
'Europe/London' => ['jan' => '2026-01-15 12:00 +00:00', 'jul' => '2026-07-15 13:00 +01:00'],
'Africa/Cairo' => ['jan' => '2026-01-15 14:00 +02:00', 'jul' => '2026-07-15 15:00 +03:00'],
'Pacific/Auckland' => ['jan' => '2026-01-16 01:00 +13:00', 'jul' => '2026-07-16 00:00 +12:00'],
];
$cases[] = [
'name' => 'fixed_utc_instant_conversions',
'passed' => $convActual === $convExpected,
'expected' => $convExpected,
'actual' => $convActual,
];
// Case 3: Non-integer UTC offsets (30-min and 45-min offsets)
$fracZones = ['Asia/Kolkata', 'Asia/Kathmandu', 'Australia/Eucla', 'Pacific/Chatham'];
$fracActual = [];
foreach ($fracZones as $z) {
$tz = new DateTimeZone($z);
$fracActual[$z] = [
'jan_offset' => $winterUtc->setTimezone($tz)->format('P'),
'jul_offset' => $summerUtc->setTimezone($tz)->format('P'),
];
}
$fracExpected = [
'Asia/Kolkata' => ['jan_offset' => '+05:30', 'jul_offset' => '+05:30'],
'Asia/Kathmandu' => ['jan_offset' => '+05:45', 'jul_offset' => '+05:45'],
'Australia/Eucla' => ['jan_offset' => '+08:45', 'jul_offset' => '+08:45'],
'Pacific/Chatham' => ['jan_offset' => '+13:45', 'jul_offset' => '+12:45'],
];
$cases[] = [
'name' => 'half_hour_and_quarter_hour_zones',
'passed' => $fracActual === $fracExpected,
'expected' => $fracExpected,
'actual' => $fracActual,
];
// Case 4: Representative identifiers from all 10 groups + DST transitions in 2026
$samples = [
'Africa/Abidjan',
'America/Adak',
'Antarctica/Casey',
'Arctic/Longyearbyen',
'Asia/Aden',
'Atlantic/Azores',
'Australia/Sydney',
'Europe/Amsterdam',
'Indian/Antananarivo',
'Pacific/Apia',
];
$validSamples = true;
foreach ($samples as $z) {
if ((new DateTimeZone($z))->getName() !== $z) {
$validSamples = false;
}
}
$start2026 = (new DateTimeImmutable('2026-01-01T00:00:00Z'))->getTimestamp();
$end2026 = (new DateTimeImmutable('2026-12-31T23:59:59Z'))->getTimestamp();
$nyTrans = (new DateTimeZone('America/New_York'))->getTransitions($start2026, $end2026);
$shTrans = (new DateTimeZone('Asia/Shanghai'))->getTransitions($start2026, $end2026);
$transActual = [
'all_ten_groups_valid' => $validSamples,
'ny_2026_transition_entries' => count($nyTrans),
'shanghai_2026_transition_entries' => count($shTrans),
];
$transExpected = [
'all_ten_groups_valid' => true,
'ny_2026_transition_entries' => 3,
'shanghai_2026_transition_entries' => 1,
];
$cases[] = [
'name' => 'timezone_validation_and_transitions',
'passed' => $transActual === $transExpected,
'expected' => $transExpected,
'actual' => $transActual,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-ref-timezones-lab/explore-ref-timezones.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-timezones.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1301.zip