0
votes

I'm trying to convert the timestamp value to date format in twig file but I'm getting the following error.

{{ form_row(form.startDate)|date("m/d/Y") }}

Error:

An exception has been thrown during the rendering of a template ("DateTime::__construct(): Failed to parse time string (<div><label for="event_setup_startDate" class="required">Start date</label><input type="text" id="event_setup_startDate" name="event_setup[startDate]" required="required" class="datetimepicker" value="1478257800" /></div>) at position 0 (<): Unexpected character")

1
You are passing the HTML code generated by the form_row function to the date constructor. Are you using a framework?Matteo
@Matteo. I'm using symfony framework. The datetype is set is string and trying to conver it to datetime format.user1965773

1 Answers

3
votes

You should pass the option format to the DateTime form field, in the form builder as example:

$builder->add('startDate', DateType::class, array(
    'format' => 'm/d/Y',
));

Or pass to the twig option as example:

{{ form_row(form.startDate, {'format': 'm/d/Y'}) }}

Hope this help