In my entity, i have two fields, one type DateTime and other type Time.
I'm trying to do this:
$time = "05:45";
$date = "01-09-2015"
$entity = new Entity();
$entity->setDate(new \DateTime($date));
$entity->setTime($time);
In both case, Symfony told me that the code has problems with format.
I also tried:
->setDate(new Datetime($date));
->setDate(DateTime::createFromFormat($date, 'd-m-Y));
->setTime(strtotime($time));
Always with the same result.
If anybody could help me, i'll be thankful.
Thanks
UPDATED
Now, I tried
$date = strtotime($date);
$time = strtotime($time);
$entity->setDate(date("d-m-Y", $date));
$entity->setTime(date("H:i",$time));
And Symfony told me:
Error: Call to a member function format() on a non-object
in vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/TimeType.php at line 53 -
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value !== null)
? $value->format($platform->getTimeFormatString()) : null;
}
/**
I also tried this:
$dateObject = new DateTime();
$date = $dateObject->format('d-m-Y');
$time = $dateObject->format('H:i');
$entity->setDate($date);
$entity->setTime($time);
And symfony told me:
Attempted to call method "format" on class "Symfony\Component\Validator\Constraints\DateTime". On line "$date = $dateObject->format('Y-m-d');"
Thanks for the help
Updated - Resolve
Finally, yesterday a resolved the problem, using this:
$entity->new Entity();
$entity->setDate(\DateTime::createFromFormat('d-m-Y',$date));
$entity->setTime(\DateTime::createFromFormat('H:i',$time));
Thanks!