*
* xkY Y (full year) in Thai solar calendar. Months and days are
* identical to the Gregorian calendar
- * xoY Y (full year) in Minguo calendar. Months and days are
+ * xoY Y (full year) in Minguo calendar or Juche year.
+ * Months and days are identical to the
+ * Gregorian calendar
+ * xtY Y (full year) in Japanese nengo. Months and days are
* identical to the Gregorian calendar
*
* Characters enclosed in double quotes will be considered literal (with
$hijri = false;
$thai = false;
$minguo = false;
+ $tenno = false;
for ( $p = 0; $p < strlen( $format ); $p++ ) {
$num = false;
$code = $format[$p];
$code .= $format[++$p];
}
- if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' ) && $p < strlen( $format ) - 1 ) {
+ if ( ( $code === 'xi' || $code == 'xj' || $code == 'xk' || $code == 'xm' || $code == 'xo' || $code == 'xt' ) && $p < strlen( $format ) - 1 ) {
$code .= $format[++$p];
}
$num = $hebrew[0];
break;
case 'xkY':
- if ( !$thai ) $thai = self::tsToThai( $ts );
+ if ( !$thai ) $thai = self::tsToYear( $ts, 'thai' );
$num = $thai[0];
break;
case 'xoY':
- if ( !$minguo ) $minguo = self::tsToMinguo( $ts );
+ if ( !$minguo ) $minguo = self::tsToYear( $ts, 'minguo' );
$num = $minguo[0];
break;
+ case 'xtY':
+ if ( !$tenno ) $tenno = self::tsToYear( $ts, 'tenno' );
+ $num = $tenno[0];
+ break;
case 'y':
$num = substr( $ts, 2, 2 );
break;
}
/**
- * Algorithm to convert Gregorian dates to Thai solar dates.
+ * Algorithm to convert Gregorian dates to Thai solar dates,
+ * Minguo dates or Minguo dates.
*
* Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
+ * http://en.wikipedia.org/wiki/Minguo_calendar
+ * http://en.wikipedia.org/wiki/Japanese_era_name
*
- * @param $ts String: 14-character timestamp
+ * @param $ts String: 14-character timestamp, calender name
* @return array converted year, month, day
*/
- private static function tsToThai( $ts ) {
+ private static function tsToYear( $ts, $cName ) {
$gy = substr( $ts, 0, 4 );
$gm = substr( $ts, 4, 2 );
$gd = substr( $ts, 6, 2 );
- # Add 543 years to the Gregorian calendar
- # Months and days are identical
- $gy_thai = $gy + 543;
-
- return array( $gy_thai, $gm, $gd );
- }
-
- /**
- * Algorithm to convert Gregorian dates to Minguo dates.
- *
- * Link: http://en.wikipedia.org/wiki/Minguo_calendar
- *
- * @param $ts String: 14-character timestamp
- * @return array converted year, month, day
- */
- private static function tsToMinguo( $ts ) {
- $gy = substr( $ts, 0, 4 );
- $gm = substr( $ts, 4, 2 );
- $gd = substr( $ts, 6, 2 );
-
- # Deduct 1911 years from the Gregorian calendar
- # Months and days are identical
- $gy_minguo = $gy - 1911;
+ if (!strcmp($cName,'thai')) {
+ # Thai solar dates
+ # Add 543 years to the Gregorian calendar
+ # Months and days are identical
+ $gy_offset = $gy + 543;
+ } else if ((!strcmp($cName,'minguo')) || !strcmp($cName,'juche')) {
+ # Minguo dates
+ # Deduct 1911 years from the Gregorian calendar
+ # Months and days are identical
+ $gy_offset = $gy - 1911;
+ } else if (!strcmp($cName,'tenno')) {
+ # Minguo dates up to Showa period
+ # Deduct years from the Gregorian calendar
+ # depending on the nengo periods
+ # Months and days are identical
+ if (($gy < 1989) || (($gy == 1989) && ($gm == 1) && ($gd < 8))) {
+ # Shōwa period
+ $gy_gannen = $gy - 1926 + 1;
+ $gy_offset = $gy_gannen + $gy_offset;
+ if ($gy_gannen == 1)
+ $gy_offset = '元';
+ $gy_offset = '昭和'.$gy_offset;
+ } else {
+ # Heisei period
+ $gy_gannen = $gy - 1989 + 1;
+ $gy_offset = $gy_gannen + $gy_offset;
+ if ($gy_gannen == 1)
+ $gy_offset = '元';
+ $gy_offset = '平成'.$gy_offset;
+ }
+ } else {
+ $gy_offset = $gy;
+ }
- return array( $gy_minguo, $gm, $gd );
+ return array( $gy_offset, $gm, $gd );
}
/**
$defaultDateFormat = 'zh';
$dateFormats = array(
- 'zh time' => 'H:i',
- 'zh date' => 'Y年n月j日 (l)',
- 'zh both' => 'Y年n月j日 (D) H:i',
+ 'zh time' => 'H:i',
+ 'zh date' => 'Y年n月j日 (l)',
+ 'zh both' => 'Y年n月j日 (D) H:i',
- 'minguo time' => 'H:i',
- 'minguo date' => 'xoY年n月j日 (l)',
- 'minguo both' => 'xoY年n月j日 (D) H:i',
+ 'minguo time' => 'H:i',
+ 'minguo date' => 'xoY年n月j日 (l)',
+ 'minguo both' => 'xoY年n月j日 (D) H:i',
'CNS 7648 time' => 'H:i',
'CNS 7648 date' => 'R.O.C. xoY-m-d (l)',