IntlDateFormatter example


    protected function formatValue(mixed $value, int $format = 0): string
    {
        if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
            if (class_exists(\IntlDateFormatter::class)) {
                $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');

                return $formatter->format(new \DateTime(
                    $value->format('Y-m-d H:i:s.u'),
                    new \DateTimeZone('UTC')
                ));
            }

            return $value->format('Y-m-d H:i:s');
        }

        if ($value instanceof \UnitEnum) {
            
$slot->setConfig([]);
        $slot->setFieldConfig($fieldConfig);

        $this->textResolver->enrich($slot$resolverContext$result);

        /** @var TextStruct|null $textStruct */
        $textStruct = $slot->getData();
        static::assertInstanceOf(TextStruct::class$textStruct);
        $content = $textStruct->getContent();
        static::assertIsString($content);

        $formatter = new \IntlDateFormatter($request->getLocale(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM);
        $actualReleaseDate = new \DateTime();
        $actualReleaseDate->setTimestamp((int) $formatter->parse($content));

        static::assertEquals($releaseDate$actualReleaseDate);
    }
}

    protected function formatValue(mixed $value, int $format = 0): string
    {
        if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
            if (class_exists(\IntlDateFormatter::class)) {
                $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');

                return $formatter->format(new \DateTimeImmutable(
                    $value->format('Y-m-d H:i:s.u'),
                    new \DateTimeZone('UTC')
                ));
            }

            return $value->format('Y-m-d H:i:s');
        }

        if ($value instanceof \UnitEnum) {
            
$dayOptions['invalid_message'] = $options['invalid_message'];
                $monthOptions['invalid_message'] = $options['invalid_message'];
                $yearOptions['invalid_message'] = $options['invalid_message'];
            }

            if (isset($options['invalid_message_parameters'])) {
                $dayOptions['invalid_message_parameters'] = $options['invalid_message_parameters'];
                $monthOptions['invalid_message_parameters'] = $options['invalid_message_parameters'];
                $yearOptions['invalid_message_parameters'] = $options['invalid_message_parameters'];
            }

            $formatter = new \IntlDateFormatter(
                \Locale::getDefault(),
                $dateFormat,
                $timeFormat,
                // see https://bugs.php.net/66323                 class_exists(\IntlTimeZone::class, false) ? \IntlTimeZone::createDefault() : null,
                $calendar,
                $pattern
            );

            // new \IntlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/66323             if (!$formatter) {
                

    protected function getIntlDateFormatter(bool $ignoreTimezone = false): \IntlDateFormatter
    {
        $dateFormat = $this->dateFormat;
        $timeFormat = $this->timeFormat;
        $timezone = new \DateTimeZone($ignoreTimezone ? 'UTC' : $this->outputTimezone);

        $calendar = $this->calendar;
        $pattern = $this->pattern;

        $intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault()$dateFormat$timeFormat$timezone$calendar$pattern ?? '');

        // new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/66323         if (!$intlDateFormatter) {
            throw new TransformationFailedException(intl_get_error_message()intl_get_error_code());
        }

        $intlDateFormatter->setLenient(false);

        return $intlDateFormatter;
    }

    
$smartDetect = false;
        }

        return $value;
    }

    protected function resolveEntityValueToString(?Entity $entity, string $path, EntityResolverContext $resolverContext): string
    {
        $content = $this->resolveEntityValue($entity$path);

        if ($content instanceof \DateTimeInterface) {
            $dateFormatter = new \IntlDateFormatter(
                $resolverContext->getRequest()->getLocale(),
                \IntlDateFormatter::MEDIUM,
                \IntlDateFormatter::MEDIUM
            );
            $content = $dateFormatter->format($content);
        }

        if ($content === null || \is_scalar($content) || (\is_object($content) && \method_exists($content, '__toString'))) {
            return (string) $content;
        }

        
static::assertStringContainsString(
                    $container->get(CurrencyFormatter::class)->formatCurrencyByLanguage(
                        $order->getAmountTotal(),
                        $order->getCurrency()->getIsoCode(),
                        Context::createDefaultContext()->getLanguageId(),
                        Context::createDefaultContext(),
                    ),
                    $rendered->getHtml()
                );

                static::assertNotNull($locale = $order->getLanguage()->getLocale());
                $formatter = new \IntlDateFormatter($locale->getCode(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE);
                $formattedDate = $formatter->format(new \DateTime());

                static::assertNotFalse($formattedDate);
                static::assertStringContainsString(
                    sprintf('Date %s', $formattedDate),
                    $rendered->getHtml()
                );
            },
        ];

        yield 'render with different language' => [
            [
$expectedTimeZoneID" raw_offset: $expectedTimeZoneRawOffset dst_savings: $expectedTimeZoneDSTSavings } } EOTXT;
        $this->assertDumpEquals($expected$var);
    }

    public function testCastDateFormatter()
    {
        $var = new \IntlDateFormatter('en', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL);

        $expectedLocale = $var->getLocale();
        $expectedPattern = $var->getPattern();
        $expectedCalendar = $var->getCalendar();
        $expectedTimeZoneId = $var->getTimeZoneId();
        $expectedTimeType = $var->getTimeType();
        $expectedDateType = $var->getDateType();

        $expectedTimeZone = $var->getTimeZone();
        $expectedTimeZoneDisplayName = $expectedTimeZone->getDisplayName();
        $expectedTimeZoneID = $expectedTimeZone->getID();
        
Home | Imprint | This part of the site doesn't use cookies.