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 code I have:
function date_japan() {
$dy = date("w");
$dys = array("日","月","火","水","木","金","土");
$dyj = $dys[$dy];
echo date('Y') . '年 ' . date('m') . '月 ' . date('d') . '日' . '(' . $dyj . ')';
}
date_japan();
Any improvements would be much appreciated. Thanks.
The easiest and most pragmatic way is probably to create a wrapper function around the date function:
function date_japan() {
echo date('Y') . '年 ' . date('m') . '月 ' . date('d') . '日';
}
This post is pretty old, but in case someone is still looking for an answer, this worked well for me:
setlocale(LC_ALL, "ja_JP.utf8");
$date_format = "%Y年%B%e日(%a)";
$date_string = strftime($date_format, time())
here is what i used...Ripped from Cameron's code ;-)
$days = array("日","月","火","水","木","金","土");
$date = @date('Y年 m月 d日 (').($dys[@date("w")]).('曜) ').@date('H:i');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With