php intldateFormatter-删除“天”从日期格式开始

发布于 2025-02-02 20:54:31 字数 779 浏览 4 评论 0原文

我想从图案中删除“天”,以显示正确的INTL日期,格式为“ F,y”。 如何在$ temppattern变量中使用点(和其他符号)的str_replace-ing“ d”字符正确地正确执行?

我不能使用$ formatter-> setPattern(),因为每种语言的日期格式都不同。

function getLocaleDateFormate($locale) {
  $formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE);
  // replace M with L: stand-alone month in year
  $tempPattern = $formatter->getPattern();
  $tempPattern = str_replace("M", "L", $tempPattern);
  return $tempPattern;
}

echo getLocaleDateFormate("sk"); // d. LLLL y
echo getLocaleDateFormate("hu"); // y. LLLL d.
echo getLocaleDateFormate("de"); // d. LLLL y
echo getLocaleDateFormate("es"); // d 'de' LLLL 'de' y
echo getLocaleDateFormate("pl"); // d LLLL y
echo getLocaleDateFormate("cn"); // y年L月d

I want to remove "day" from the pattern to display the correct INTL date in format "F, Y".
How to correctly do it without str_replace-ing "d" character with dots (and other symbols) in the $tempPattern variable?

I can't use $formatter->setPattern(), as the date format is different for each language.

function getLocaleDateFormate($locale) {
  $formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE);
  // replace M with L: stand-alone month in year
  $tempPattern = $formatter->getPattern();
  $tempPattern = str_replace("M", "L", $tempPattern);
  return $tempPattern;
}

echo getLocaleDateFormate("sk"); // d. LLLL y
echo getLocaleDateFormate("hu"); // y. LLLL d.
echo getLocaleDateFormate("de"); // d. LLLL y
echo getLocaleDateFormate("es"); // d 'de' LLLL 'de' y
echo getLocaleDateFormate("pl"); // d LLLL y
echo getLocaleDateFormate("cn"); // y年L月d

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2025-02-09 20:54:31

输出不是正确的顺序(它迫使每种语言的“ llll y”顺序),因此我对特定语言进行了更正。我不完全满意,但没有更好的解决方案。

if ($locale == "hu") // corrections may apply for specific languages
    $formatter->setPattern("y. LLLL");
elseif ($locale == "cn")
    $formatter->setPattern("y年L月");
else
    $formatter->setPattern("LLLL y"); // stand-alone-monthName Year
return $formatter->getPattern();

The output is not in the correct order (it forces the 'LLLL y' order for each language), so I applied the corrections for specific languages. I am not satisfied fully but I don't have any better solution.

if ($locale == "hu") // corrections may apply for specific languages
    $formatter->setPattern("y. LLLL");
elseif ($locale == "cn")
    $formatter->setPattern("y年L月");
else
    $formatter->setPattern("LLLL y"); // stand-alone-monthName Year
return $formatter->getPattern();
清晰传感 2025-02-09 20:54:31

我最终创建了一个可以在当地格式化日期的函数,希望能提供帮助!

  const LANG_BACK = 'fr';
  const TIMEZONE = 'Europe/Paris';  

   /**
     * format_date_locale( $date_Object, $dateType , $timeType, $pattern );
     *
     * @param  {object} $date_Object  DateTime object or object accepted
     * @param  {string} $dateType       'NONE', 'SHORT', 'MEDIUM', 'LONG', 'FULL'
     * @param  {string} $timeType       'NONE', 'SHORT', 'MEDIUM', 'LONG', 'FULL'
     * @param  {pattern} $pattern       'MMMM y' -> may 2022
     * see how to set patterns: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
     * @return {string}                     date formated locally
     */
    format_date_locale( $date_Object, $dateType , $timeType, $pattern ){

            // date format
            switch ( $dateType  ) {
                case 'NONE':
                    $Date_Format = IntlDateFormatter::NONE; // 20220606 08:16 AM
                break;
                case 'SHORT':
                    $Date_Format = IntlDateFormatter::SHORT; // 06/06/2022
                break;
                case 'MEDIUM':
                    $Date_Format = IntlDateFormatter::MEDIUM; // 6 juin 2022 in [fr] must vary
                break;
                case 'LONG':
                    $Date_Format = IntlDateFormatter::LONG; // 6 juin 2022
                break;
                case 'FULL':
                    $Date_Format = IntlDateFormatter::FULL; // lundi 6 juin 2022
                break;

                default:
                    $Date_Format = IntlDateFormatter::SHORT;
                break;
            }

            // time format
            switch ( $timeType  ) {
                case 'NONE':
                    $Time_Format = IntlDateFormatter::NONE; // ''
                break;
                case 'SHORT':
                    $Time_Format = IntlDateFormatter::SHORT; // 08:11
                break;
                case 'MEDIUM':
                    $Time_Format = IntlDateFormatter::MEDIUM; // 08:11:10
                break;
                case 'LONG':
                    $Time_Format = IntlDateFormatter::LONG; // 08:09:33 UTC+2
                break;
                case 'FULL':
                    $Time_Format = IntlDateFormatter::FULL; // 08:10:38 heure d’été d’Europe centrale
                break;

                default:
                    $Time_Format = IntlDateFormatter::SHORT;
                break;
            }

            // create date formatter
            //  LANG_BACK, TIMEZONE, -> const API
            $local_date = IntlDateFormatter::create(
                LANG_BACK, // lang
                $Date_Format, // date
                $Time_Format, // time
                TIMEZONE, // timezone
                null, // type of calendar / null -> const IntlDateFormatter::GREGORIAN
                $pattern // pattern to apply or null
            );

            // return date formatted
            return $local_date->format( $date_Object );

    }
    /**
     * end format_date_locale( $date, $dateType , $timeType, $pattern );
     */

使用 :

  $date_Object = new DateTime('now', new DateTimeZone(TIMEZONE) );
  echo format_date_locale( $date_Object, 'FULL' , 'SHORT', null );
  // output : jeudi 9 juin 2022 15:38

I ended up creating a function that would format dates locally, hoping to help !

  const LANG_BACK = 'fr';
  const TIMEZONE = 'Europe/Paris';  

   /**
     * format_date_locale( $date_Object, $dateType , $timeType, $pattern );
     *
     * @param  {object} $date_Object  DateTime object or object accepted
     * @param  {string} $dateType       'NONE', 'SHORT', 'MEDIUM', 'LONG', 'FULL'
     * @param  {string} $timeType       'NONE', 'SHORT', 'MEDIUM', 'LONG', 'FULL'
     * @param  {pattern} $pattern       'MMMM y' -> may 2022
     * see how to set patterns: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
     * @return {string}                     date formated locally
     */
    format_date_locale( $date_Object, $dateType , $timeType, $pattern ){

            // date format
            switch ( $dateType  ) {
                case 'NONE':
                    $Date_Format = IntlDateFormatter::NONE; // 20220606 08:16 AM
                break;
                case 'SHORT':
                    $Date_Format = IntlDateFormatter::SHORT; // 06/06/2022
                break;
                case 'MEDIUM':
                    $Date_Format = IntlDateFormatter::MEDIUM; // 6 juin 2022 in [fr] must vary
                break;
                case 'LONG':
                    $Date_Format = IntlDateFormatter::LONG; // 6 juin 2022
                break;
                case 'FULL':
                    $Date_Format = IntlDateFormatter::FULL; // lundi 6 juin 2022
                break;

                default:
                    $Date_Format = IntlDateFormatter::SHORT;
                break;
            }

            // time format
            switch ( $timeType  ) {
                case 'NONE':
                    $Time_Format = IntlDateFormatter::NONE; // ''
                break;
                case 'SHORT':
                    $Time_Format = IntlDateFormatter::SHORT; // 08:11
                break;
                case 'MEDIUM':
                    $Time_Format = IntlDateFormatter::MEDIUM; // 08:11:10
                break;
                case 'LONG':
                    $Time_Format = IntlDateFormatter::LONG; // 08:09:33 UTC+2
                break;
                case 'FULL':
                    $Time_Format = IntlDateFormatter::FULL; // 08:10:38 heure d’été d’Europe centrale
                break;

                default:
                    $Time_Format = IntlDateFormatter::SHORT;
                break;
            }

            // create date formatter
            //  LANG_BACK, TIMEZONE, -> const API
            $local_date = IntlDateFormatter::create(
                LANG_BACK, // lang
                $Date_Format, // date
                $Time_Format, // time
                TIMEZONE, // timezone
                null, // type of calendar / null -> const IntlDateFormatter::GREGORIAN
                $pattern // pattern to apply or null
            );

            // return date formatted
            return $local_date->format( $date_Object );

    }
    /**
     * end format_date_locale( $date, $dateType , $timeType, $pattern );
     */

Use :

  $date_Object = new DateTime('now', new DateTimeZone(TIMEZONE) );
  echo format_date_locale( $date_Object, 'FULL' , 'SHORT', null );
  // output : jeudi 9 juin 2022 15:38
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文