Skip to content

Instantly share code, notes, and snippets.

@fetus-hina
Last active April 17, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fetus-hina/5d01a08cc464b252f056b0daa4a555e5 to your computer and use it in GitHub Desktop.
Save fetus-hina/5d01a08cc464b252f056b0daa4a555e5 to your computer and use it in GitHub Desktop.
PHPで和暦表示
<?php
// ICU version => 62.1
// ICU Data version => 62.1
// ICU TZData version => 2018e
// ICU Unicode version => 11.0
declare(strict_types=1);
$tz = new DateTimeZone('Asia/Tokyo');
$times = [
new DateTimeImmutable('2019-04-30T23:59:59', $tz), // 平成の最後
new DateTimeImmutable('2019-05-01T00:00:00', $tz), // 令和の最初
new DateTimeImmutable('2020-01-01T00:00:00', $tz), // なんか適当な未来
new DateTimeImmutable('2100-01-01T00:00:00', $tz), // すごい未来
];
$dateFormatter = IntlDateFormatter::create(
"ja_JP@calendar=japanese",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
$tz,
IntlDateFormatter::TRADITIONAL,
'Gyy年MM月dd日 HH時mm分ss秒' // 平成xx年
);
foreach ($times as $t) {
printf(
"%s: %s\n",
$t->format(DateTime::ATOM),
$dateFormatter->format($t),
);
}
echo "====================\n";
$dateFormatter = IntlDateFormatter::create(
"ja_JP@calendar=japanese",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
$tz,
IntlDateFormatter::TRADITIONAL,
'Gy年MM月dd日 HH時mm分ss秒' // 平成xxxx年
);
foreach ($times as $t) {
printf(
"%s: %s\n",
$t->format(DateTime::ATOM),
$dateFormatter->format($t),
);
}
2019-04-30T23:59:59+09:00: 平成31年04月30日 23時59分59秒
2019-05-01T00:00:00+09:00: 平成31年05月01日 00時00分00秒
2020-01-01T00:00:00+09:00: 平成32年01月01日 00時00分00秒
2100-01-01T00:00:00+09:00: 平成12年01月01日 00時00分00秒
====================
2019-04-30T23:59:59+09:00: 平成31年04月30日 23時59分59秒
2019-05-01T00:00:00+09:00: 平成31年05月01日 00時00分00秒
2020-01-01T00:00:00+09:00: 平成32年01月01日 00時00分00秒
2100-01-01T00:00:00+09:00: 平成112年01月01日 00時00分00秒
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment