Japanese Date in this format

后端 未结 5 1463
醉酒成梦
醉酒成梦 2020-12-17 23:52

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

相关标签:
5条回答
  • 2020-12-18 00:14

    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.

    0 讨论(0)
  • 2020-12-18 00:20
    $date_japan = date('Y年m月d日', date("w"));
    
    0 讨论(0)
  • 2020-12-18 00:27

    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') . '日';
    }
    
    0 讨论(0)
  • 2020-12-18 00:28

    here is what i used...Ripped from Cameron's code ;-)

    $days = array("日","月","火","水","木","金","土");
    $date = @date('Y年 m月 d日 (').($dys[@date("w")]).('曜) ').@date('H:i');
    
    0 讨论(0)
  • 2020-12-18 00:29

    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())
    
    0 讨论(0)
提交回复
热议问题