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

transform() public method

Transforms a DateTime object into a date string with the configured format and timezone
public transform ( DateTime $value ) : string
$value DateTime A DateTime object
return string A value as produced by PHP's date() function
    public function transform($value)
    {
        if ($value === null) {
            return '';
        }

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

        $value->setTimezone(new \DateTimeZone($this->getOption('output_timezone')));

        return $value->format($this->getOption('format'));
    }

Usage Example

 public function testTransformExpectsDateTime()
 {
     $transformer = new DateTimeToStringTransformer();
     $this->setExpectedException('Symfony\\Component\\Form\\Exception\\UnexpectedTypeException');
     $transformer->transform('1234');
 }
All Usage Examples Of Symfony\Component\Form\ValueTransformer\DateTimeToStringTransformer::transform