此格式的日本日期 [英] Japanese Date in this format

查看:354
本文介绍了此格式的日本日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要以这种格式用日语显示日期:

I'm wanting show the Date in Japanese in this format:

2010年2月18日(木)

2010年2月18日(木)

其翻译为:

2010年2月18日(星期四)

February 18, 2010 (Thu)

在PHP

这是我的代码:

    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.

推荐答案

使用 IntlDateFormatter ,您可以设置任何(受支持的)语言的格式.

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";

这应该给你

2010年2月18日(木)

2010年2月18日(木)

,您还可以获得另一种具有不同语言环境名称的语言.

and you can also get another language with different locale names.

如果您对格式没问题

2010年2月18日木曜日

2010年2月18日木曜日

PHP(和PHP内部调用的ICU库)认为完整的日语日期的正确格式,代码会更简单.像这样

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.

这篇关于此格式的日本日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆