Set DateTime and Time in Symfony2 from String

19,619

Solution 1

Symfony doesn't know where to find DateTime class, so it is searching in the wrong (default) namespace. Use a leading \.

Moreover you inverted the format argument and the time argument in the createFromFormat() function.

So your code becomes:

$time = "05:45";
$date = "01-09-2015"

$entity = new Entity();
$entity->setDate(\DateTime::createFromFormat('d-m-Y', $date));
$entity->setTime(\DateTime::createFromFormat('H:i', $time));

By the way, are you sure you really need to split Date and Time in your entity? It depends on your case but you could do this instead (only one object to manipulate):

$time = "05:45";
$date = "01-09-2015"

$entity = new Entity();
$entity->setDateTime(\DateTime::createFromFormat('d-m-Y H:i', $date.' '.$time));

Solution 2

Check out http://php.net/manual/de/datetime.createfromformat.php to be sure that your format fits.

Or check mktime(hour,minute,second,month,day,year) Or check strtotime(time,now) e.g. here http://www.w3schools.com/php/php_date.asp

Solution 3

Do it like this:

$dateObject = new DateTime();
$date = $dateObject->format('Y-m-d');
$time = $dateObject->format('H:i:s'); // or $time = $dateObject->format('H:i'); if you dont want to have the seconds

$entity = new Entity();
$entity->setDate($date);
$entity->setTime($time);
Share:
19,619
jekeyeke
Author by

jekeyeke

Updated on June 13, 2022

Comments

  • jekeyeke
    jekeyeke almost 2 years

    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!

  • jekeyeke
    jekeyeke over 8 years
    Thank you for the code, buy know Symfony throw this error: Attempted to call method "format" on class "Symfony\Component\Validator\Constraints\DateTime". On line "$date = $dateObject->format('Y-m-d');"
  • Reformat Code
    Reformat Code over 8 years
    Instead of $dateObject = new DateTime(); use: $dateObject = new \DateTime(); // see the \ for the DateTime
  • jekeyeke
    jekeyeke over 8 years
    Hi! Yes, i need to split Date and Time. I updated the post with the final result that i got last night. It's very similar than your andswer. Thanks.
  • jekeyeke
    jekeyeke over 8 years
    Finally I resolved it with createFromFormat and it works perfect. Thanks!