Japanese Imperial Era Year (Reiwa/Heisei/Showa) Regex for PHP
/^(?:(令和|R)((?:[1-9]|[1-9][0-9]))|(平成|H)((?:[1-9]|[12][0-9]|3[01]))|(昭和|S)((?:[1-9]|[12][0-9]|3[0-9]|6[0-4])))年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching japanese imperial era year (reiwa/heisei/showa), ported and verified for PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Japanese Imperial Era Year (Reiwa/Heisei/Showa)
// ReDoS-safe | RegexVault — Localization > Date Formats
define('JAPANESE_IMPERIAL_ERA_YEAR_REIWAHEISEISHOWA_PATTERN', '/^(?:(令和|R)((?:[1-9]|[1-9][0-9]))|(平成|H)((?:[1-9]|[12][0-9]|3[01]))|(昭和|S)((?:[1-9]|[12][0-9]|3[0-9]|6[0-4])))年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日$/');
function validate_japanese_imperial_era_year_reiwaheiseishowa(string $input): bool {
return (bool) preg_match(JAPANESE_IMPERIAL_ERA_YEAR_REIWAHEISEISHOWA_PATTERN, $input);
}
// Example
var_dump(validate_japanese_imperial_era_year_reiwaheiseishowa("令和6年1月15日")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
令和6年1月15日 | 令和0年1月1日 |
R6年3月31日 | R100年1月1日 |
平成31年4月30日 | 明治30年1月1日 |
H31年1月1日 | 2024年1月15日 |
昭和64年1月7日 | — |
When to use this pattern
This pattern is drawn from the Localization > Date Formats category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Era year 1 of the new era and the last year of the previous era overlap — both are valid for the year of transition. 平成31年 and 令和元年 are both 2019.
Technical Notes
Era limits: 令和/Reiwa started May 1, 2019 (max year currently ~6). 平成/Heisei: 1-31 (1989-2019). 昭和/Showa: 1-64 (1926-1989). Meiji and Taisho are pre-1926 and excluded here. Convert to Gregorian for storage.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern