Skip to content
Advertisement

How could I cast to DateTimeInterface from String?? Symfony Ajax

My real problem is that I am sending an Ajax Jquery Request to an API of mine, the server response is ‘Argument #1 of ::setDate() must be DateTimeInterface, string given’

I tried to cast but it was useless.

My Ajax at JQUERY:

JavaScript

My API at PHP (Symfony’s controller):

JavaScript

The Object I’m sending:

The console.log of the object sended

Advertisement

Answer

Your entity Distribucion probably has a setter that look like this:

JavaScript

The setter is typehinted with DateTimeInterface so you need to use a DateTime object and not a string.

You may create a DateTime easily with DateTime::createFromFormat

So $distribucion->setFecha($datos->fecha); will turn into something like:

JavaScript

You may want to verify the date format I used as the first parameter of DateTime::createFromFormat

public static DateTime::createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement