I\'m wanting show the Date in Japanese in this format:
2010年2月18日(木)
which translates to:
February 18, 2010 (Thu)
in PHP
This is the
With IntlDateFormatter, you may format any (well, supported) languages.
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
exit ('IntlDateFormatter is available on PHP 5.3.0 or later.');
}
if (!class_exists('IntlDateFormatter')) {
exit ('You need to install php_intl extension.');
}
$longFormatter = new IntlDateFormatter(
'ja_JP',
IntlDateFormatter::LONG,
IntlDateFormatter::NONE
);
$weekdayFormatter = new IntlDateFormatter(
'ja_JP',
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
date_default_timezone_get(),
IntlDateFormatter::GREGORIAN,
'EEEEE' // weekday in one letter
);
$datetime = new DateTime("2010-02-18");
echo $longFormatter->format($datetime)
. '(' . $weekdayFormatter->format($datetime) . ")\n";
This should give you,
2010年2月18日(木)
and you can also get another language with different locale names.
If you are okay with the format
2010年2月18日木曜日
which PHP (and ICU library PHP internally calls) thinks the proper format for full Japanese date, the code would be simpler. Like this,
$fullFormatter = new IntlDateFormatter(
'ja_JP',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE
);
$datetime = new DateTime("2010-02-18");
echo $fullFormatter->format($datetime) . "\n";
Then, you will never worry when you need to add more languages support in future. Your code will be free from Japanese-specific handling.