Symfony\Component\Form\ValueTransformer\DateTimeToLocalizedStringTransformer::transform PHP Method

transform() public method

Transforms a normalized date into a localized date string/array.
public transform ( DateTime $dateTime ) : string | array
$dateTime DateTime Normalized date.
return string | array Localized date string/array.
    public function transform($dateTime)
    {
        if ($dateTime === null) {
            return '';
        }

        if (!$dateTime instanceof \DateTime) {
            throw new \InvalidArgumentException('Expected value of type \DateTime');
        }

        $inputTimezone = $this->getOption('input_timezone');

        // convert time to UTC before passing it to the formatter
        if ($inputTimezone != 'UTC') {
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
        }

        $value = $this->getIntlDateFormatter()->format((int)$dateTime->format('U'));

        if (intl_get_error_code() != 0) {
            throw new TransformationFailedException(intl_get_error_message());
        }

        return $value;
    }

Usage Example

 public function testTransformRequiresValidDateTime()
 {
     $transformer = new DateTimeToLocalizedStringTransformer();
     $this->setExpectedException('\\InvalidArgumentException');
     $transformer->transform('2010-01-01');
 }
All Usage Examples Of Symfony\Component\Form\ValueTransformer\DateTimeToLocalizedStringTransformer::transform