PHPで西暦を和暦の表記に変換するためのコードです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| <?php
echo convert_japanese_year(2000, 1, 1); //「H12」を出力
function convert_japanese_year($year, $month, $day) {
$date = (int)sprintf("%04d%02d%02d", $year, $month, $day);
if ($date >= 19890108) { //平成元年(1989年1月8日以降)
$name = "H";
$year -= 1988;
} else if ($date >= 19261225) { //昭和元年(1926年12月25日以降)
$name = "S";
$year -= 1925;
} else if ($date >= 19120730) { //大正元年(1912年7月30日以降)
$name = "T";
$year -= 1911;
} else if ($date >= 18680125) { //明治元年(1868年1月25日以降)
$name = "M";
$year -= 1867;
}
return $name.(string)$year;
}
?> |
コメントはまだありません。
この投稿へのコメントの RSS フィード。
TrackBack URL
|