Created
April 10, 2012 08:10
-
-
Save mgng/2349259 to your computer and use it in GitHub Desktop.
YYYYMMDDから和暦求めるようなやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function toWareki ( $y, $m, $d ) { | |
| $data = array( | |
| array( 'nengou' => '明治', 'from' => 18680908, 'to' => 19120730,), | |
| array( 'nengou' => '大正', 'from' => 19120730, 'to' => 19261225,), | |
| array( 'nengou' => '昭和', 'from' => 19261225, 'to' => 19890107,), | |
| array( 'nengou' => '平成', 'from' => 19890107, 'to' => 99999999,), | |
| ); | |
| $year = $y; | |
| $nengou = ''; | |
| $ymd = (int)sprintf( '%04d%02d%02d', $y, $m, $d ); | |
| foreach( $data as $d ) { | |
| if ( $d['from'] <= $ymd && $ymd < $d['to'] ) { | |
| $nengou = $d['nengou']; | |
| $year = $y - ( (int)substr( (string)$d['from'], 0, 4) - 1 ); | |
| break; | |
| } | |
| } | |
| return array( | |
| 'nengou' => $nengou, | |
| 'year' => $year, | |
| ); | |
| } | |
| print_r( toWareki(2012, 4, 30) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment