使いまわしていた和暦変換処理。
令和になって既に半年、需要がほとんどなくなりましたが、今回使う機会があったので、判定処理に追加しました。
//西暦 → 和暦 変換関数
function convWareki($src) {
list($year, $month, $day) = explode("/", $src);
if (!@checkdate($month, $day, $year) || $year < 1869 || strlen($year) !== 4
|| strlen($month) !== 2 || strlen($day) !== 2) return false;
$date = str_replace("/", "", $src);
$gengo = "";
$wayear = 0;
if ($date >= 20190501) {
$gengo = '令和';
$wayear = $year - 2018;
} elseif ($date >= 19890108) {
$gengo = "平成";
$wayear = $year - 1988;
} elseif ($date >= 19261225) {
$gengo = "昭和";
$wayear = $year - 1925;
} elseif ($date >= 19120730) {
$gengo = "大正";
$wayear = $year - 1911;
} else {
$gengo = "明治";
$wayear = $year - 1868;
}
switch ($wayear) {
case 1:
$wadate = $gengo."元年".ltrim($month, "0")."月".ltrim($day, "0")."日";
break;
default:
$wadate = $gengo.sprintf("%02d", $wayear)."年".ltrim($month, "0")."月".ltrim($day, "0")."日";
}
return $wadate;
}
echo convWareki(2019/12/5);
echo convWareki(date('Y/m/d'));
コメント